Comparing a string (case sensitive)

This is a discussion on Comparing a string (case sensitive) within the C++ Programming forums, part of the General Programming Boards category; I am trying to compare the string addPayType to all case sensitive possibilities. I have tried the strupr(), and a ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    11

    Question Comparing a string (case sensitive)

    I am trying to compare the string addPayType to all case sensitive possibilities. I have tried the strupr(), and a few other i have found on this board with 0% success rate. Here is my code snipit, Remember addPayType is string class. I did #include <string> and ctype.h in trying all the possiblilties found on this board. This is my first post so please forgive if im enter it incorrectly.

    Code:
    if(addPayType == "salary" || addPayType == "Salary" || addPayType == "SALARY")

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    strcmp(); makes it alot easier to compare 2 strings

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I think you want case insensitivity, first of all. You could do this:

    Example
    Code:
    #include <cstring>
    
    char temp[256];
    
    std::strcpy(temp, addPayType.c_str());
    std::strupr(temp);
    
    if(std::strcmp(temp, "SALARY") == 0)  {
      // do stuff if temp equals "SALARY"
    }

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    11

    Does strcmp() worry about the case of the letters in the string when comparing??

    I accidentally posted new thread last time i trie to ask LOL

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    11
    Yes master you are right i did intend for it to be case insensitive.

    can anyone tell me if strcmp() is a case sensitive compare or not ?

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,804
    Direct from a book:
    Code:
    #include <string>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    bool nocase_compare( char c1, char c2 )
    {
        return toupper(c1) == toupper(c2);
    }
    
    int main()
    {
        string s1("Hello");
        string s2("hElLo");
    
        if( s1.size() == s2.size() &&
            equal( s1.begin(), s1.end(), s2.begin(), nocase_compare) )
                cout << "The strings are equal." << endl;
        else cout << "The strings are not equal." << endl;
    
        return 0;
    }
    The size comparison in the "if" clause is needed otherwise two strings of unequal length will cause undefined behavior within the equal function. The equal function will go from the beginning of strings s1 and s2 through to the ends of the strings and apply the given nocase_compare function to each element. Haven't tested it but it looks OK. Hope this helps.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    11

    Thank you all for your help..

    I went with the strcmp() option presented by master5001 and it works fine. this is what the snipit looks like now. Ty all

    Code:
    #include <cstring.h>
    
    char temp[9];
    
                    cin >> addPayType;
    
    	strcpy(temp, addPayType.c_str());
    
    	strupr(temp);
    
    	if(strcmp(temp, "HOURLY") == 0)
    
                    {
    
                             // do stuff;
    
                    }

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    you dont need to use strupr(), there is a version of strcmp that is not CaSe SeNsAtIvE, its stricmp()
    "There are three kinds of people in the world...
    Those that can count and those that can't."

  9. #9
    Registered User
    Join Date
    Feb 2003
    Posts
    11

    again i thank you for your help

    as advised earlier i tried strcmp() then after reading al ittle farther i noticed the strcmpi() and now i see stricmp(). Which is the correct ver, or are they both correct. I used strcmpi() and it works fine.

  10. #10
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    yea, my mistake, they are both correct
    "There are three kinds of people in the world...
    Those that can count and those that can't."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  2. char copy
    By variable in forum C Programming
    Replies: 8
    Last Post: 02-06-2005, 09:18 PM
  3. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 PM
  4. Changing bkgrnd color of Child windows
    By cMADsc in forum Windows Programming
    Replies: 11
    Last Post: 09-10-2002, 11:21 PM
  5. A simple array question
    By frenchfry164 in forum C++ Programming
    Replies: 7
    Last Post: 11-25-2001, 03:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21