954,479 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

toupper and tolower implementation for whole c-strings

0
By Mathias Van Malderen on May 22nd, 2009 4:48 pm

This is a toupper and tolower implementation for whole c-strings, without making use of the existing toupper and tolower functions for characters :)

example:

char s[]="Hello, beautiful world!";
stoupper(s); // s now contains: `HELLO, BEAUTIFUL WORLD!´
stolower(s); // s now contains: `hello, beautiful world!´
/**
@author: Mathias Van Malderen (tux4life)
*/

void stoupper(char *s)
{
    for(; *s; s++)
        if(('a' <= *s) && (*s <= 'z'))
            *s = 'A' + (*s - 'a');
}

void stolower(char *s)
{
    for(; *s; s++)
        if(('A' <= *s) && (*s <= 'Z'))
            *s = 'a' + (*s - 'A');
}

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
 

Works only for ASCII. Your résumé will be uppercased to RéSUMé or bungle the e with accent. Nice, isn't it?

nalply
Newbie Poster
8 posts since May 2009
Reputation Points: 10
Solved Threads: 1
 

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
 

unsigned char string[];

///////// Convert entire string: uppercase to lowercase ////////

for (i=0; i

AuSsIeStOnE
Newbie Poster
3 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

On Windows, use Shell apis (1 line of code...)

mika_
Newbie Poster
6 posts since Apr 2009
Reputation Points: 7
Solved Threads: 0
 

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
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You