Home > front end >  How can I make this anagram test case-insensitive?
How can I make this anagram test case-insensitive?

Time:12-19

how can I make this anagram code check regardless whether the words are capital or not.
The code looks like this:

Enter first word: Abc
Enter second word: aBc
not anagram

but it should also be an anagram pls help thanks

#include<iostream>
using namespace std;

int main (){
    string firstWord, secondWord;
    bool isAnagram;

    cout<<"Enter the first word: ";
    cin>>firstWord;

    cout<<"Enter the second word: ";
    cin>>secondWord;

    if (firstWord.length() == secondWord.length()){
    
        for (int i = 0; i < firstWord.length (); i  ){
        
            for (int j = 0; j < firstWord.length(); j  ){
            
                if (firstWord[i] == secondWord[j]){
                
                    isAnagram = true;
                    break;
                }    
                else isAnagram = false;          
            }
            if (isAnagram == false) break;
        }

        if (isAnagram == true) cout<<"anagram\n";
        else cout<<"not anagram\n";
    } 
}

CodePudding user response:

#include<iostream>
#include <cctype>
using namespace std;

int main (){
    string firstWord, secondWord;
    bool isAnagram = true;

    cout<<"Enter the first word: ";
    cin>>firstWord;

    cout<<"Enter the second word: ";
    cin>>secondWord;

    if (firstWord.length() == secondWord.length()){
    
        for (int i = 0; i <= (firstWord.length ())/2; i  ){
        
            for (int j = secondWord.length(); j <=secondWord.length()/2 ; j--){
            
                if (tolower(firstWord[i]) != tolower(secondWord[j])){
                
                    isAnagram = false;
                    break;
                }    
            }
            
        }

        if (isAnagram == true) cout<<"anagram\n";
        else cout<<"not anagram\n";
    } 
}

CodePudding user response:

Convert the characters to lower case before you compare them

            if (tolower(firstWord[i]) == tolower(secondWord[j])){

The tolower function can be found in #include <cctype>

Also not sure about your algorithm, why do you only check the first half of firstWord against the second half of secondWord? It's like you have mixed up the algorithm for a palindrome with the algorithm for an anagram.

The way to test for an anagram is to sort the strings (case-insensitively) and then see if the string are equal (again case-insensitively).

CodePudding user response:

Checking whether 2 strings are anagrams (i.e. contain the same letters) using your approach has quadratic O(n^2) complexity due to the nested for loops.

It is more efficient to sort both strings first (this can be done in O(nlogn)), then simply compare them. The overall complexity will be O(nlogn).

Before sorting you need to convert both strings to lower case if you want a case insensitve comparison.

#include <iostream>
#include <algorithm>
#include <cctype>

void string_tolower(std::string const& in, std::string & out)
{
    out.resize(in.length());
    std::transform(in.begin(), in.end(), out.begin(),
        [](unsigned char c) { return std::tolower(c); });
}

int main() {
    std::string word1 = "bAc";
    std::string word2 = "aBc";

    // Convert to lower case:
    std::string word1lower;
    string_tolower(word1, word1lower);
    std::string word2lower;
    string_tolower(word2, word2lower);

    // Sort lower case strings:
    auto compCharInsensitive = [](char c1, char c2) { return std::tolower(c1) < std::tolower(c2); };
    std::sort(word1lower.begin(), word1lower.end(), compCharInsensitive);
    std::sort(word2lower.begin(), word2lower.end(), compCharInsensitive);

    // Determine result:
    bool isAnagram = (word1lower == word2lower);
    if (isAnagram == true) 
        std::cout << "anagram\n";
    else 
        std::cout << "not anagram\n";
}

Output:

anagram

Note that if you don't need to keep the original strings you can re-use them instead of word1lower, word2lower. string_tolower can be used to do the change in place (i.e. with in the same as out).

Side note: better to avoid using namespace std - see here Why is "using namespace std;" considered bad practice?.

  •  Tags:  
  • c
  • Related