| | |
convert string to lower case
Thread Solved |
•
•
Join Date: Feb 2007
Posts: 3
Reputation:
Solved Threads: 0
as above.
for example, if the user enters Orange, i want it to convert to orange.
please advise what i should do.
thanks in advance
C++ Syntax (Toggle Plain Text)
string s; cin >> s;
please advise what i should do.
thanks in advance
•
•
Join Date: Jul 2005
Posts: 1,898
Reputation:
Solved Threads: 302
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]);
inputString[i] = tolower(inputString[i]);
•
•
•
•
as above.
for example, if the user enters Orange, i want it to convert to orange.C++ Syntax (Toggle Plain Text)
string s; cin >> s;
please advise what i should do.
thanks in advance
C++ Syntax (Toggle Plain Text)
string UpToLow(string str) { for (int i=0;i<strlen(str.c_str());i++) if (str[i] >= 0x41 && str[i] <= 0x5A) str[i] = str[i] + 0x20; return str; }
Good luck, LamaBot
Last edited by Lazaro Claiborn; Feb 28th, 2007 at 1:49 pm.
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
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*
•
•
•
•
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
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
•
•
•
•
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.
Good luck, LamaBot
Last edited by Lazaro Claiborn; Feb 28th, 2007 at 3:36 pm.
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:
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*
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Ok, so whats error 1073741515 again?
- Next Thread: Classes anyone please?
Views: 62315 | Replies: 19
| Thread Tools | Search this Thread |
Tag cloud for C++
algorithm api array arrays assignment basic beginner binary browser c++ c/c++ calculator char class classes code compile compiler constructor conversion convert count data delete desktop display dll dynamic encryption error file files form fstream function functions game givemetehcodez graph gui homework i/o iamthwee input int integer lazy library linker list loop loops map math matrix member memory network newbie news number object objects opengl output parameter pointer pointers problem program programming project random read recursion recursive reference sort sorting spoonfeeding string strings struct student studio template templates text time tree undefined variable vc++ vector video visual win32 window windows winsock






