Shorter, but perhaps not any more efficient.
void stoupper(char *s) {
do if (96 == (224 & *s)) *s &= 223;
while (*s++);
}
Wow, I'm competitive. No doubt there's some mistake in mine though.
Here's how it works:
the first lowercase letter 'a' has a binary of: 01100001
the last lowercase letter 'z' has a binary of: 01111010
Both these letters have 01100000 in common.
But if the bit 10000000 is on, then it cant be a letter.
So by doing an AND check with 11100000 and comparing (==) it with the value 01100000, it should determine whether or not the character is a valid lowercase letter.
Then remove the bit 00100000 to make the character a capital.
Am I good or what? :D
William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
Obviously only works with ascii and doesn't support unicode, otherwise char* would have been used. Either way, using the toupper function there outputs: RÚSUMÚ for me (even though it's most probably just a font problem). I would rather it printed RéSUMé :] Both these codes simply turn any characters between 'a' to 'z' and makes them uppercase.
William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
Hi, naply, take a look at this and you'll see that toupper only converts alphabet letters to uppercase (this is analog for tolower), I intended my code to do 'exactly' the same :)
WH>Am I good or what?
Yes, you're the best :P
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
Or just use XOR 32 to swap the values of a valid range. Ta-da...
MosaicFuneral
Posting Virtuoso
1,691 posts since Nov 2008
Reputation Points: 888
Solved Threads: 116
This is the implementation, firstly you can't do this in one line, and even if you could nobody would care.
William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129