3

Possible Duplicate:
Case insensitive string comparison in C++

I've written some code for a c++ to compare two strings for equality. What I'd like is a proofreading. I'm planning on using this for more programs in the future, so it's important that this function do its job well. Does this function look like one that will be reusable, portable, etc.? Is there a more 'up-to-date' method of doing this? I used a c library but this is a c++ program, is that taboo?

Thanks, JH.

//function to compare two strings regardless of case 
//-- returns true if the two strings are equal
//-- returns false if
//  --the strings are unequal
//  --one of the strings is longer than 255 chars
bool isEqual(string str1, string str2){

  if(str1.length()!=str2.length())  //the strings are different lengths,
    return false;               //they can't be equal
  if((str1.length()>255) || (str2.length()>255))
    return false;

  char * cstr1 = new char [str1.length()+1];
  strcpy (cstr1, str1.c_str());
  char * cstr2 = new char [str2.length()+1];
  strcpy (cstr2, str2.c_str());

  for(int i=0; i<str1.length()+1; i++){
    if(toupper(cstr1[i]) != toupper(cstr2[i]))
      return false;
  }

  return true;
}
Community
  • 1
  • 1
Jefferson Hudson
  • 728
  • 1
  • 10
  • 22

2 Answers2

20

You should rename the function to isEqual_CaseInsensitive or something to have a name that matches what the function does. You should pass the strings by reference to avoid a copy You don't need to create a copy of the strings to compare them

    bool isEqual_CaseInsensitive(const string& a, const string& b)
    {
            return a.size() == b.size() &&
                std::equal(a.begin(), a.end(), b.begin(), [](char cA, char cB) {
                        return toupper(cA) == toupper(cB);
                   });
    }
anonymous
  • 471
  • 4
  • 11
  • Thank you very much. That is an elegant use of the return keyword. – Jefferson Hudson Sep 24 '12 at 15:55
  • Here's a question about this function:[](char cA, char cB) { return toupper(cA) == toupper(cB); }... is this function nameless? or is it named [], or is [] indicative of the return type of this function? It seems to be a boolean function after all (and the std::equal requires that, right? http://www.cplusplus.com/reference/algorithm/equal/) – Jefferson Hudson Sep 24 '12 at 16:04
  • +1. In case this question will be marked as duplicate: there is no such elegant solution there: http://stackoverflow.com/questions/11635/case-insensitive-string-comparison-in-c, on the other hand the question was from 2008... – PiotrNycz Sep 24 '12 at 16:10
  • 1
    @JeffersonHudson That is a lambda function which is part of c++11. The [] denotes an empty closure and the function has no name at all. See http://msdn.microsoft.com/en-us/library/dd293608.aspx or google around for more info on lambdas – Matt Sep 24 '12 at 16:28
2

The function looks quite good except that:

These conversions to c strings are not necessary:

  char * cstr1 = new char [str1.length()+1];
  strcpy (cstr1, str1.c_str());
  char * cstr2 = new char [str2.length()+1];
  strcpy (cstr2, str2.c_str());

since you can access of std::string letters like in c string:

  for(int i=0; i<str1.length(); i++){
    if(toupper(str1[i]) != toupper(str2[i]))
      return false;
  }

also note that I removed +1 from i<str1.length() + 1...

For other methods - see there: Case insensitive string comparison in C++

Community
  • 1
  • 1
PiotrNycz
  • 23,099
  • 7
  • 66
  • 112