Search code examples
c++for-loopanagram

C++ How to ignore the difference between upper and lower when checking Anagram?


bool isAnagram(string s1, string s2){
   if(s1.length() != s2.length())
      return false;

  for(int i =0; i <s1.length();i++){
      int pos = (s2.find(s1[i]);
                 if(pos<0)
                 return false;
                 s2.erase(pos,1);

               }
                 return true;

                 return false;

               }

I write the code about checking Anagram but ignore the requirement of ignoring difference between upper and lower. I have no idea about that. Can you help me? Thanks!


Solution

  • Convert both to lowercase before comparing

    std::transform(s1.begin(), s1.end(), s1.begin(), ::tolower);
    std::transform(s2.begin(), s2.end(), s2.begin(), ::tolower);