Thread: String Case

  1. #1
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719

    String Case

    What's the command for strings to ignore case? Is it strincmp? I want to compare user input to a coded command such as, if you want to execute a command, type 'RUN', but say the user types 'run', I want the program to ignore the case, so it will recognize it. I think changing the user input to all CAPS or all lower letters would suffice, but I don't know the command. GRACIAS!

  2. #2
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    tolower()?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  3. #3
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    I thought this was for char only? Can this read in a lengthy string?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Case-insensitive C style string comparison is usually done with strcmpi or stricmp, but those are non-standard.

    You could change the string to all upper case using a single call to to the transform algorithm (and using toupper or tolower). Search for sample code since there is some small nuances due to the differences between std::toupper and ::toupper.

  5. #5
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    HMMMMMM......How about 'ToUpper()' instead of 'toupper' it seems like it is better for handling strings. Anyone think it's better?

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I would use one of these:
    • strupper()
    • strtoupper()
    • toupperstr()

    Of course, I'm a C programmer.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> How about 'ToUpper()' instead of 'toupper' it seems like it is better for handling strings.
    Never heard of it. If you're using .NET, then it might be better, but then why are you posting here?

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >there is some small nuances due to the differences between std::toupper and ::toupper.
    That's interesting Daved. I tried transform using std::toupper, and got a compile error, and tried ::toupper and it compiled. Presumably std::toupper only saw the int version while ::toupper was able to locate the char version.

    I guess there's a difference between the location of std::toupper vs ::toupper.

  9. #9
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Quote Originally Posted by Daved
    >> How about 'ToUpper()' instead of 'toupper' it seems like it is better for handling strings.
    Never heard of it. If you're using .NET, then it might be better, but then why are you posting here?
    I'm not using .NET, just trying to throw some ideas around.

    I also got a compiler error when using std::toupper(), I don't understand that 'int' conversion thingy. But the other one worked!

  10. #10
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Well, toupper() is looking like a very odd function! What's the deal with this integer thing? Shouldn't the parameter accept the data type of 'string'? It can only convert one char, so how is an actual word changed to upper case with this function?

  11. #11
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Umm...a loop?

    Here's a simple C program that makes use of toupper(), in case you don't understand:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void)
    {
      char str[] = "OMG This iS sOmE SCRewy CRap";
      char *p;
    
      for(p = str;*p;p++)
        *p = toupper(*p);
    
      puts(str);
    
      return 0;
    }
    Code:
    itsme@itsme:~/C$ ./upperize
    OMG THIS IS SOME SCREWY CRAP
    itsme@itsme:~/C$
    Last edited by itsme86; 09-07-2006 at 05:27 PM.
    If you understand what you're doing, you're not learning anything.

  12. #12
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    O rly? I know that. But it's just inconvenient to have to create a loop and all that crap! Is it possible to create your own function similar to 'toupper' but with a string parameter? How can I look at the actual 'toupper' function code?

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I'm not using .NET, just trying to throw some ideas around.
    If the ToUpper you were referring to is the .NET function, and you aren't using .NET, then I don't think it will work too well.

    >> how is an actual word changed to upper case with this function?
    It isn't. You change each character one at a time with either a loop or the transform algorithm I already mentioned. Using transform will allow you to do the whole string in a single line.

  14. #14
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Is it possible to create your own function similar to 'toupper' but with a string parameter?
    Of course. And yes, it might contain a loop.
    How can I look at the actual 'toupper' function code?
    It's doubtful that you have the source for it on your computer. You could always try google of course to find some code that does it.
    If you understand what you're doing, you're not learning anything.

  15. #15
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Ok, thanks! Me love you long time! Me give you EVERYTING LONG TIME!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. Replies: 5
    Last Post: 03-05-2009, 11:32 AM
  3. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM