Convert userinput to lowercase
Sep 11, 2010 at 7:08am UTC
Hi,
I'm having a problem with converting user input to lower case.
I'm using getline to get user input, here is my code:
1 2 3 4 5 6
int main(){
getline (cin,input);
for (int i=0;i<strlen(input);i++){
input++; //<---
}
cout<<"Your input is:" <<input<<endl;
There's some logic bugs in the line I'm pointing at,if someone can help me with it.
Thanks a lot
Sep 11, 2010 at 8:30am UTC
1 2 3 4 5 6 7 8 9 10 11 12
string input; // use C++ style string with getline
int main(){
getline (cin,input);
for (int i=0;i<input.length();i++){ // input.length() gets the length of the string
// convert every character of input to lowercase ( I think there are other methods to do this)
input[i]=tolower(input[i]);
}
cout<<"Your input is:" <<input<<endl;
}
Last edited on Sep 11, 2010 at 8:30am UTC
Sep 11, 2010 at 10:29am UTC
Thanks a lot!
I didn't know how the tolower use...
Thanks for your help.
Sep 11, 2010 at 1:48pm UTC
You could also use the STL.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#include <algorithm> // transform()
#include <cctype> // toupper(), tolower()
#include <functional> // ptr_fun()
#include <iostream> // cin, cout
#include <string> // getline(), string
using namespace std;
int main()
{
cout << "Enter something: " << flush;
string s;
getline( cin, s );
transform( s.begin(), s.end(), s.begin(), ptr_fun <int , int > ( toupper ) );
cout << "Uppercase: " << s << endl;
transform( s.begin(), s.end(), s.begin(), ptr_fun <int , int > ( tolower ) );
cout << "Lowercase: " << s << endl;
return 0;
}
Enjoy!
Topic archived. No new replies allowed.