Link to home
Start Free TrialLog in
Avatar of Tom Knowlton
Tom KnowltonFlag for United States of America

asked on

tolower done manually

I know that ctype.h has a tolower function.

If I wanted to write one manually...how is it done?

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of viktornet
viktornet
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nietod
nietod

Why use ASCII numeric values?  how about

char ToLower(char ch)
   {
     if (ch >= 'A' && ch <= 'Z')
       return (ch+'a'-'A');
   }
same thing :))

ch + 32 is gonna be executed faster than ch + 'a' - 'A' :))
Personally, I have no faith in optimizers, but even the worst optimizer had better catch that.
with today's pentium II, there will not be much difference between the two..
Avatar of ozo
both make an assumption about your locale
'A' is more obvious than 65 (and makes fewer assumtions about your character set)
Avatar of Tom Knowlton

ASKER

Viktornet:

Your answer sounded good to me.  Haven't actually tried it yet, but I'm sure it works.

Thanks,

Tom
This reminds me of the assembly days, when I used a bit operation to convert to lower or upper case.
These macro should do the work

#define ToUpper(ch) (((ch) >= 'a' && (ch) <= 'z') ? ((ch) & ~0x20) : (ch))
#define ToLower(ch) (((ch) >= 'A' && (ch) <= 'Z') ? ((ch) | 0x20) : (ch))

Regards,

Wenderson