Go Premium for a chance to win a PS4. Enter to Win

x
Solved

# tolower done manually

Posted on 1999-01-20
Medium Priority
258 Views
I know that ctype.h has a tolower function.

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

Thanks!
0
Question by:Tom Knowlton

LVL 10

Accepted Solution

viktornet earned 80 total points
ID: 1184652
char ToLower(char ch)
{
if (ch >= 65 && ch <= 90)
return (ch+32);
}

char c = 'K';

cout << ToLower(c);

Hope this helps...

-Viktor
--Ivanov
0

LVL 22

Expert Comment

ID: 1184653
Why use ASCII numeric values?  how about

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

LVL 10

Expert Comment

ID: 1184654
same thing :))

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

LVL 22

Expert Comment

ID: 1184655
Personally, I have no faith in optimizers, but even the worst optimizer had better catch that.
0

LVL 10

Expert Comment

ID: 1184656
with today's pentium II, there will not be much difference between the two..
0

LVL 85

Expert Comment

ID: 1184657
'A' is more obvious than 65 (and makes fewer assumtions about your character set)
0

LVL 5

Author Comment

ID: 1184658
Viktornet:

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

Thanks,

Tom
0

Expert Comment

ID: 1184659
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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generatâ€¦
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there isâ€¦
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Botâ€¦
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
###### Suggested Courses
Course of the Month13 days, 7 hours left to enroll