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; } 
5
  • Ooops, didn't see you want it to be case insensitive. Commented Sep 24, 2012 at 15:42
  • 5
    For starters, you leak memory. That's a bad thing. Commented Sep 24, 2012 at 15:46
  • 1
    you never delete cstr1 and cstr2, thus leaking their memory. I don't see what you need those copies for though, since you don't change them anyways. Commented Sep 24, 2012 at 15:48
  • For every new, there must be a delete. Or use of smart pointers. Commented Sep 24, 2012 at 15:49
  • @LuchianGrigore possible duplicate is from 2008. There are more elegant way to compare case insensitive strings now: see anonymous's answer. Commented Sep 24, 2012 at 16:12

2 Answers 2

21

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); }); } 
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you very much. That is an elegant use of the return keyword.
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? cplusplus.com/reference/algorithm/equal)
+1. In case this question will be marked as duplicate: there is no such elegant solution there: stackoverflow.com/questions/11635/…, on the other hand the question was from 2008...
@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 msdn.microsoft.com/en-us/library/dd293608.aspx or google around for more info on lambdas
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++

2 Comments

@PiotrNycz For this, you'll also want to confirm they are the same length.
@loganfsmyth - I assumed the rest of the code is unchanged. And test for equal length is there.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.