Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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