Thread: Strings

  1. #1
    Registered User Ajsan's Avatar
    Join Date
    Dec 2003
    Posts
    55

    Strings

    is there a function that will convert all the letters in a string to upper or lower case to make it easier to make a check like "if (a=="JUMP")".

    i've tryed toupper().
    Style is overrated.

    - —₽‚¢‰Î -

  2. #2
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    Do you mean an std::string or a char[].
    If the first, I think you could do
    Code:
    std::string a("JUMP"), b("jump");
    //not sure about the capitalization of std::string::toupper()
    if(a == b.toupper()){
    //do whatever
    }
    If the second, you can't compare it like that. use strcmp() like so
    Code:
    char a[5] = "JUMP",
           b[5] = "jump";
    
    toupper(b);
    
    if(!strcmp(a, b)){
    //code
    }
    The `!' is there because strcmp will return 0 is the strings are equal, -1 if the 1st is "bigger", and 1 if the 2nd is "bigger".
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  3. #3
    Registered User Ajsan's Avatar
    Join Date
    Dec 2003
    Posts
    55
    a std::string...
    Style is overrated.

    - —₽‚¢‰Î -

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    "toupper()" is not a member of basic_string. The C function toupper() operates on a single character.

    The easiest thing to do is a case insensitive comparison with stricmp(), if it's available in your CRT library (it's a non-standard function).

    gg

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >is there a function that will convert all the letters in a string to upper or lower case
    Combine toupper or tolower with the transform template function in <algorithm>:
    Code:
    #include <algorithm>
    #include <cctype>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
      string s ( "test" );
    
      cout<< s <<endl;
      transform ( s.begin(), s.end(), s.begin(), toupper );
      cout<< s <<endl;
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM