Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 261
  • Last Modified:

tolower done manually

I know that ctype.h has a tolower function.

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

Thanks!
0
Tom Knowlton
Asked:
Tom Knowlton
1 Solution
 
viktornetCommented:
char ToLower(char ch)
{
  if (ch >= 65 && ch <= 90)
    return (ch+32);
}

char c = 'K';

cout << ToLower(c);

Hope this helps...

-Viktor
--Ivanov
0
 
nietodCommented:
Why use ASCII numeric values?  how about

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

ch + 32 is gonna be executed faster than ch + 'a' - 'A' :))
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
nietodCommented:
Personally, I have no faith in optimizers, but even the worst optimizer had better catch that.
0
 
viktornetCommented:
with today's pentium II, there will not be much difference between the two..
0
 
ozoCommented:
both make an assumption about your locale
'A' is more obvious than 65 (and makes fewer assumtions about your character set)
0
 
Tom KnowltonWeb developerAuthor Commented:
Viktornet:

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

Thanks,

Tom
0
 
casaprogCommented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now