Thread: Comparing Strings

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    70

    Comparing Strings

    Hey,

    What i want to do is Compare two Strings, but when i compare them -- i want to make it compare Lower Case and Uppercase Letters. For Example, if i wanted to compare two strings:

    str1 = "Str";
    str2 = "str";

    I want to know how i could evaluate these two strings, but make it compare upper and lowercase letters as the same thing -- so if i compared the two strings it would say they were equal; I want it to ignore upper and lowercase letters!!

    Is there a function that does this included in the Standard C/C++ Library (NOTE: I use MSVC++ 6); If there is, could somebody please give me an example on how to use it.....

    If there isn't, then how could i do this...??

    I hope my Question is Clear,
    Cya.

  2. #2
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    I think theres a function in the <string> header (strupr()) or something, you'll find it on this site somewhere in the totorial section on strings.

    But what would I know, I'm the guy that said 2 / 2 = square root of 2.

    Paul

  3. #3
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    The function you're thinking of is toupper, defined in ctype.h IIRC. Convert your strings to upper case (or copies of them if you want to leave the originals unchanged). Then use strcmp to compare them (in string.h).

    [edit]
    I'm pretty sure strupr is non-standard
    [/edit]
    Demonographic rhinology is not the only possible outcome, but why take the chance

  4. #4
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    This question has been convered many times. A search Here and a reference page will answer you question.

  5. #5
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>I want it to ignore upper and lowercase letters!!
    If you don't mind using a nonstandard function for convenience then this should work fine for you :-)
    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int main()
    {
      char str1[] = "Str";
      char str2[] = "str";
    
      if (strcmpi(str1, str2) == 0)
      {
        cout<<"The same!"<<endl;
      }
      
      return 0;
    }
    *Cela*

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    70
    Alright, thanks for the Help!

    I'll Create my Own Function that Converts letters to lower-case.

    Cya.

  7. #7
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Code:
    void upperstring(std::string& convert)
    {
       std::transform(
           convert.begin(), 
           convert.end(),
           convert.begin(),
           ::toupper
       );
    }
    
    void lowerstring(std::string& convert)
    {
       std::transform(
           convert.begin(), 
           convert.end(),
           convert.begin(),
           ::tolower
       );
    }
    The STL makes it easy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. comparing 2 strings with pointer
    By meriororen in forum C Programming
    Replies: 9
    Last Post: 05-22-2009, 07:37 PM
  2. Replies: 2
    Last Post: 04-29-2009, 10:13 AM
  3. Problem with comparing strings!
    By adrian2009 in forum C Programming
    Replies: 2
    Last Post: 02-28-2009, 10:44 PM
  4. comparing strings using argv
    By eth0 in forum C Programming
    Replies: 2
    Last Post: 09-20-2005, 09:20 AM
  5. comparing strings
    By infinitum in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2003, 12:10 PM