|
-
February 22nd, 2007, 04:03 AM
#1
converting input into lowercase?
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
-
February 22nd, 2007, 04:05 AM
#2
Re: converting input into lowercase?
did u checked out MSDN have a look for the function isUpper() toLower() .
Example from MSDN
Code:
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
Last edited by humptydumpty; February 22nd, 2007 at 04:13 AM.
-
February 22nd, 2007, 04:27 AM
#3
Re: converting input into lowercase?
Last edited by SuperKoko; February 22nd, 2007 at 07:24 AM.
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
February 22nd, 2007, 05:12 AM
#4
Re: converting input into lowercase?
 Originally Posted by swiftys
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 
Code:
#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
-
February 22nd, 2007, 05:43 AM
#5
Re: converting input into lowercase?
thank you very much for your help guys.
cheers!
-
February 23rd, 2007, 06:38 AM
#6
Re: converting input into lowercase?
 Originally Posted by Paul McKenzie
Code:
#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:
Code:
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
-
February 23rd, 2007, 08:53 AM
#7
Re: converting input into lowercase?
 Originally Posted by alnikolov
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):
Code:
#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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|