Kodewerx https://www.kodewerx.org/forum/ |
|
a-z --> A-Z https://www.kodewerx.org/forum/viewtopic.php?f=5&t=6 |
Page 1 of 1 |
Author: | James0x57 [ Sun Oct 01, 2006 10:28 pm ] |
Post subject: | a-z --> A-Z |
C++ I tried this: Code: if ((letter >= 33)&&(letter <= 58)) But it didn't work. Is there an easier way? ^_^
{ letnum = int(letter) + 32; letter = char(letnum); } |
Author: | Parasyte [ Sun Oct 01, 2006 10:33 pm ] |
Post subject: | |
This is a part of my "xstring" lib, which does upper case/lower case conversion, white space trimming, character replacement, sub-string searching, etc. Code: // Upper case routine
// Returns number of characters modified int str_ucase(char *str) { int i = 0; int j = 0; while (i < strlen(str)) { if ((str[i] >= 'a') && (str[i] <= 'z')) { str[i] &= ~0x20; j++; } i++; } return j; } |
Author: | ?man [ Sun Oct 01, 2006 10:34 pm ] |
Post subject: | |
Keep in mind I'm a total newbie when it comes to C, but...assuming letter is a char, shouldn't you be using int(letter) inside the comparison? EDIT: Nevermind. Para is the man when it comes to C. |
Author: | dlong [ Sun Oct 01, 2006 10:44 pm ] |
Post subject: | |
a char is a signed short int. PS: Para's post is what I would've have done, except: Code: str[i] -= 0x20; inside the for loop. Using a NOT and an AND is likely faster though.
|
Author: | Parasyte [ Sun Oct 01, 2006 10:47 pm ] |
Post subject: | |
A char is a signed char (8-bit) A short is a signed short int (16-bit) An int is a signed int (32-bit) A long is a signed long int (64-bit) A long long is a signed long long int (128-bit) Of course, this all depends upon the implementation. These numbers expect a 64-bit architecture. (For 32-bit architecture, 'long' is 32-bit, and 'long long' is 64-bit.) ~ Bitwise operations and simple addition/subtraction are equally clocked on recent CPUs. (Made within the past decade or more.) |
Author: | James0x57 [ Mon Oct 02, 2006 12:00 am ] |
Post subject: | |
Thanks for trying to help guys! I got it working though! Code: if ((letter >= 97)&&(letter <= 122))
{ letter = letter - 32; } I'm going from lower case to caps and lowers have higher values than caps so I had to subtract 32. It also worked without the type casting. ^_^ easy n sleazy woot 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 1111011110000111000000011100001111111100 0100000100011000110001100011001001100100 0100100100010000010001000001001001100100 0101110100110000011011000001101001100100 0101110100110000011011000001100001100000 0111011100110000011011000001100001100000 0111011100010000010001000001000001100000 0110001100011000110001100011000001100000 0110001100000111000000011100000111111000 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 |
Author: | Parasyte [ Mon Oct 02, 2006 12:31 am ] |
Post subject: | |
Using character constants ('a' and 'z') instead of numbers is much more readable, by the way. |
Author: | James0x57 [ Mon Oct 02, 2006 8:11 am ] |
Post subject: | |
lol If you're talking about the program code, it doesn't really matter as it is comented in the "full" program. ;p And, lol, if you mean the output of the program, it's only a mid-point for what I'd like to do. ^_^ Thanks though, Parasyte. ![]() |
Author: | Parasyte [ Mon Oct 02, 2006 8:16 am ] |
Post subject: | |
I am talking about source code readability, actually. letter >= 'a' vs. letter >= 97 What the fuck is 97? I don't know without looking it up... But I know which letter 'a' is. |
Author: | dlong [ Mon Oct 02, 2006 8:46 am ] |
Post subject: | |
Parasyte wrote: A char is a signed char (8-bit)
A short is a signed short int (16-bit) An int is a signed int (32-bit) A long is a signed long int (64-bit) A long long is a signed long long int (128-bit) Of course, this all depends upon the implementation. These numbers expect a 64-bit architecture. (For 32-bit architecture, 'long' is 32-bit, and 'long long' is 64-bit.) ~ Bitwise operations and simple addition/subtraction are equally clocked on recent CPUs. (Made within the past decade or more.) Hmm.... interesting. I had always thought that a short was a byte. Would that be the case on 32-bit? |
Author: | Parasyte [ Mon Oct 02, 2006 8:52 am ] |
Post subject: | |
'short' is always 16-bit, so far as I recall. |
Page 1 of 1 | All times are UTC - 8 hours [ DST ] |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |