User | Post |
Dish
Posts: 327/596 |
HH's way was a shorter version of mine. Or rather I should say mine was a longer method of his, since he brought it up first.
To clarify the logic behind mine (even though you already pretty much have it solved -- I still feel compelled to explain mine so that the concept is understood):
A character in single quotes (such as 'A') is a character constant. It's replaced with the numerical value representing that character at compiletime. To illustrate this with pseudo-code (assuming ASCII char set):
char ch;
ch = 'A'; //this line has the same result as the line below ch = 0x41;
if( 'A' == 0x41 ) { //this condition is true, code here would execute }
if( 'a' == 0x61 ) { //also true }
if( ('a' - 'A') == 0x20 ) { //also true }
My code essentially does the exact same thing as HH's, it just doesn't make you remember the exact numerical difference between upper and lowercase letters in the charset.
MyString[0] += 'A' - 'a';
... is the same as ...
MyString[0] += 0x41 - 0x61;
... is the same as ...
MyString[0] += -0x20;
... is the same as ...
MyString[0] -= 0x20;
The only difference is I dodged the use of a numerical constant (0x20) and used easier-to-remember character constants ('A' , 'a') with some simple logic math (add the target character, subtract the current character -- or add the difference between the two).
It's the same principle as converting a numerical charater ("0 1 2 3 4 5 6 7 8 9") to a numerical value.
char ch = '6';
int num;
num = ch - '0'; //this will yield 6 num = ch - 0x30; // this will also yield 6
They both do the same thing -- but the former doesn't require you to remember an arbitrary value. |
Nebetsu
Posts: 1241/1574 |
Thank you everyone. I used HyperHacker's method because it makes more sense than Disch's method. Sorry Disch. Your method is good and would make more sense to a normal mind, but I think HyperHacker's makes more sense because I understand how the ASCII chart works and stuff. It takes the ascii character number and subtracts 0x20 from it and then gives you the new character. Very nice method HyperHacker. |
Dish
Posts: 325/596 |
oh yeah -- I forgot about those XD
But yeah -- since C++ includes all of C, that'd work in C++ too. |
Banedon
Posts: 1143/1408 |
Not sure about C++, but I know that C has the toupper function, which should work properly even when the character is not lowercase. |
Parasyte
Posts: 418/514 |
And don't forget to check that the char is actually a lowercase letter... You don't want to 'translate' any non-alpha characters or capital letters. |
Dish
Posts: 324/596 |
Instead of -= 0x20 -- I'd just use character constants:
MyString[0] += 'A' - 'a';
so you don't have to remember an exact value (this might also work if you change charsets too -- but don't take my word on that one).
But yeah -- either way you get the result. |
HyperLamer
Posts: 4039/8210 |
I don't know if there's a specific function to do it, but something like this should work:
if((MyString[0] >= 'a') && (MyString[0] <= 'z')) //Between a and z? MyString[0] -= 0x20; //Subtract difference between 'a' and 'A'
Assuming you're using char arrays, and not some C++ thing I don't know about. |
Nebetsu
Posts: 1238/1574 |
How do I capitolise the first character of a string in C++? |