Click to See Complete Forum and Search --> : converting input into lowercase?
swiftys
February 22nd, 2007, 03:03 AM
Hey, first of all I'd just like to say sorry, sorry if this is brought up, some newb comes in asking questions that are just... stupid >.<
I'm actually pretty **** new to C++, but I'm getting there :)
I'm wondering if there is anyway I can convert someone's input in lowercase, whether it be from a variable or from when the user enters in the input.
Thanks ;)
humptydumpty
February 22nd, 2007, 03:05 AM
did u checked out MSDN have a look for the function isUpper() toLower() .
Example from MSDN
char msg[] = "Some of THESE letters are Capitals\r\n";
char *p;
void main( void )
{
_cputs( msg );
/* Reverse case of message. */
for( p = msg; p < msg + strlen( msg ); p++ )
{
if( islower( *p ) )
_putch( _toupper( *p ) );
else if( isupper( *p ) )
_putch( _tolower( *p ) );
else
_putch( *p );
}
}
Alternative is Simply read the string make a loop read each character from the string and check it's ascii value if it is between 65 to 90 simply add 32 to the original value .and store the character back to some array.
Thanx
SuperKoko
February 22nd, 2007, 03:27 AM
Use std::tolower
Paul McKenzie
February 22nd, 2007, 04:12 AM
Hey, first of all I'd just like to say sorry, sorry if this is brought up, some newb comes in asking questions that are just... stupid >.<
I'm actually pretty **** new to C++, but I'm getting there :)
I'm wondering if there is anyway I can convert someone's input in lowercase, whether it be from a variable or from when the user enters in the input.
Thanks ;)
#include <algorithm>
#include <string>
#include <cctype>
#include <iostream>
using namespace std;
int main()
{
string input;
getline(cin, input);
// convert input to lower case
transform(input.begin(), input.end(), input.begin(), std::tolower);
cout << input;
}
Regards,
Paul McKenzie
swiftys
February 22nd, 2007, 04:43 AM
thank you very much for your help guys.
cheers!
;)
alnikolov
February 23rd, 2007, 05:38 AM
#include <algorithm>
#include <string>
#include <cctype>
#include <iostream>
using namespace std;
int main()
{
string input;
getline(cin, input);
// convert input to lower case
transform(input.begin(), input.end(), input.begin(), std::tolower);
cout << input;
}
Regards,
Paul McKenzie
std::tolower requires 2 parameters and cannot be used like this.
You can use C-library tolower instead or you can do this:
char makelower( char ch)
{
return std::tolower(ch, locale());
}
int main()
{
string input;
getline(cin, input);
// convert input to lower case
transform(input.begin(), input.end(), input.begin(), makelower);
cout << input;
}
Aleksandar Nikolov
Philip Nicoletti
February 23rd, 2007, 07:53 AM
std::tolower requires 2 parameters and cannot be used like this.
You can use C-library tolower instead or you can do this:
Your method is the safest way (it will compile on all compilers). But
whether Paul's code is incorrect is unclear (at least to me).
1) see Josuttis' comments near the very top at:
http://www.josuttis.com/libbook/new.html
2) All compilers that I know of accept this (including comeau):
#include <string>
#include <cctype>
#include <algorithm>
int main()
{
std::string s = "Hello World";
std::transform(s.begin(),s.end(),s.begin(),std::toupper);
}
3) However, if you include <iostream> (among a few others headers),
g++ will give a compiler error.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.