CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2007
    Posts
    2

    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

  2. #2
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    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.

  3. #3
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: converting input into lowercase?

    Use std::tolower
    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()!

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: converting input into lowercase?

    Quote 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

  5. #5
    Join Date
    Feb 2007
    Posts
    2

    Re: converting input into lowercase?

    thank you very much for your help guys.

    cheers!


  6. #6
    Join Date
    Jan 2007
    Location
    South Africa
    Posts
    1

    Re: converting input into lowercase?

    Quote 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

  7. #7
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: converting input into lowercase?

    Quote 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
  •  





Click Here to Expand Forum to Full Width

Featured