Solved

tolower done manually

Posted on 1999-01-20
8
251 Views
Last Modified: 2010-04-02
I know that ctype.h has a tolower function.

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

Thanks!
0
Comment
Question by:knowlton
8 Comments
 
LVL 10

Accepted Solution

by:
viktornet earned 20 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

by:nietod
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

by:viktornet
ID: 1184654
same thing :))

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

Expert Comment

by:nietod
ID: 1184655
Personally, I have no faith in optimizers, but even the worst optimizer had better catch that.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 10

Expert Comment

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

Expert Comment

by:ozo
ID: 1184657
both make an assumption about your locale
'A' is more obvious than 65 (and makes fewer assumtions about your character set)
0
 
LVL 5

Author Comment

by:knowlton
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

by:casaprog
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now