convert string to lower case

Thread Solved

Join Date: Feb 2007
Posts: 3
Reputation: darkAngel is an unknown quantity at this point 
Solved Threads: 0
darkAngel darkAngel is offline Offline
Newbie Poster

convert string to lower case

 
0
  #1
Feb 28th, 2007
as above.
  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
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,898
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 302
Lerner Lerner is offline Offline
Posting Virtuoso

Re: convert string to lower case

 
0
  #2
Feb 28th, 2007
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]);
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 171
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: convert string to lower case

 
0
  #3
Feb 28th, 2007
Originally Posted by darkAngel View Post
as above.
  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:
  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 1:49 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,321
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 384
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: convert string to lower case

 
0
  #4
Feb 28th, 2007
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
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 171
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: convert string to lower case

 
0
  #5
Feb 28th, 2007
Originally Posted by iamthwee View Post
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,321
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 384
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: convert string to lower case

 
0
  #6
Feb 28th, 2007
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 3:20 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 171
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: convert string to lower case

 
0
  #7
Feb 28th, 2007
Originally Posted by iamthwee View Post
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 3:36 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,321
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 384
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: convert string to lower case

 
0
  #8
Feb 28th, 2007
str.length() <-- Brackets

:lol:
Last edited by iamthwee; Feb 28th, 2007 at 3:43 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 171
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: convert string to lower case

 
0
  #9
Feb 28th, 2007
Originally Posted by iamthwee View Post
str.length() <-- Brackets

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

:lol: :lol: :cheesy:

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


LamaBot
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,321
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 384
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: convert string to lower case

 
0
  #10
Feb 28th, 2007
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:
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 62315 | Replies: 19
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC