943,832 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 114131
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 28th, 2007
0

convert string to lower case

Expand Post »
as above.
C++ Syntax (Toggle Plain Text)
  1. string s;
  2. cin >> s;
for example, if the user enters Orange, i want it to convert to orange.
please advise what i should do.


thanks in advance
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
darkAngel is offline Offline
3 posts
since Feb 2007
Feb 28th, 2007
0

Re: convert string to lower case

There is a standard function called tolower() (and a companion function called toupper()) that can be used to evaluate each char of a string one at a time and change it to the appropriate case as needed. So to use it, look up tolower() in your compilers help section and make sure you include any necessary standard headers. Then obtain input string and determine it's length. Then loop through the string looking at each character one at a time, passing each char to tolower() as you go

inputString[i] = tolower(inputString[i]);
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Feb 28th, 2007
0

Re: convert string to lower case

Click to Expand / Collapse  Quote originally posted by darkAngel ...
as above.
C++ Syntax (Toggle Plain Text)
  1. string s;
  2. cin >> s;
for example, if the user enters Orange, i want it to convert to orange.
please advise what i should do.


thanks in advance
Here is a function that'll do it:
C++ Syntax (Toggle Plain Text)
  1. string UpToLow(string str) {
  2. for (int i=0;i<strlen(str.c_str());i++)
  3. if (str[i] >= 0x41 && str[i] <= 0x5A)
  4. str[i] = str[i] + 0x20;
  5. return str;
  6. }

Good luck, LamaBot
Last edited by Lazaro Claiborn; Feb 28th, 2007 at 2:49 pm.
Reputation Points: 11
Solved Threads: 13
Junior Poster
Lazaro Claiborn is offline Offline
171 posts
since Jan 2007
Feb 28th, 2007
0

Re: convert string to lower case

Go with lerner's idea.

Lazaro Claiborn's idea, regardless of whether it works or not, is rather obfuscated.

It also has other problems...

http://www.cprogramming.com/tips/sho...ount=30&page=0
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Feb 28th, 2007
0

Re: convert string to lower case

Click to Expand / Collapse  Quote originally posted by iamthwee ...
Go with lerner's idea.

Lazaro Claiborn's idea, regardless of whether it works or not, is rather obfuscated.

It also has other problems...

http://www.cprogramming.com/tips/sho...ount=30&page=0
Umm... obfuscated???? It is rather easy to understand, which is why I wrote it like that. Actually, strlen(str.c_str()) could be replaced with, say "str.length" perhaps. However, it is not obfuscated. Let me explain what the code does:

You pass the string you want to be converted to all lower case, which is what the OP specified
It loops and iterates through dealing with each element of the string
If the elements corresponding hex value is not in between the values within the capital alphabetcial letter range defined by ascii, it won't do anything (i.e won't convert numbers)
If it is within range it adds 20 using hex notation (i.e 0x20) from its corresponding hex value - which effectively converts it to its lowercase counterpart.

That is very easy to understand to say the least.

LamaBot
Reputation Points: 11
Solved Threads: 13
Junior Poster
Lazaro Claiborn is offline Offline
171 posts
since Jan 2007
Feb 28th, 2007
0

Re: convert string to lower case

If you say so. But you wouldn't give a newbie that.

>Actually, strlen(str.c_str()) could be replaced with, say "str.length" perhaps.

Try reading my link again. Then you might realise what you're doing wrong.
Last edited by iamthwee; Feb 28th, 2007 at 4:20 pm.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Feb 28th, 2007
0

Re: convert string to lower case

Click to Expand / Collapse  Quote originally posted by iamthwee ...
If you say so. But you wouldn't give a newbie that.

>Actually, strlen(str.c_str()) could be replaced with, say "str.length" perhaps.

Try reading my link again. Then you might realise what you're doing wrong.
Well that'd be the problem if the string contained a massive amount of characters - which you nor I know. I read the link you'd posted and I still don't realise the problem. Lol. :lol: I realised that might be problem therefore I suggested "str.length" as alternative. Overall, if he's a newbie trying to learn he probably doesn't care, yet, the specific operations of a function, such as strlen(...); not to imply it doesn't matter, becuase code optimization is vital to a programs performance. You have to consider the obvious.

Good luck, LamaBot
Last edited by Lazaro Claiborn; Feb 28th, 2007 at 4:36 pm.
Reputation Points: 11
Solved Threads: 13
Junior Poster
Lazaro Claiborn is offline Offline
171 posts
since Jan 2007
Feb 28th, 2007
0

Re: convert string to lower case

str.length() <-- Brackets

:lol:
Last edited by iamthwee; Feb 28th, 2007 at 4:43 pm.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Feb 28th, 2007
0

Re: convert string to lower case

Click to Expand / Collapse  Quote originally posted by iamthwee ...
str.length() <-- Brackets

:lol:
str.lenght() <-- Enclosing Parenthesis

:lol: :lol: :cheesy:

[[ <--- Brackets
[ <--- Opening Brackets
] <--- Closing Brackets
[] <--- Enclosing Brackets


LamaBot
Reputation Points: 11
Solved Threads: 13
Junior Poster
Lazaro Claiborn is offline Offline
171 posts
since Jan 2007
Feb 28th, 2007
0

Re: convert string to lower case

str.lenght() <-- Enclosing Parenthesis

Er not were I'm from. All I was pointing out was you forgot them when u said use str.length.

The main thing is you have learnt why it is a bad idea to use strlen() in a for loop and why it is bad to give newbies non-intuitive code as examples. So overall, everyone is happy. Yay.

:lol:
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Threading problem
Next Thread in C++ Forum Timeline: pls help me with my switch program... :(





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC