vicyap
vicyap

Reputation: 11

Determine if 2 strings are anagrams

Write a function that accepts a c-string and a character as parameters. Returns the number of times the character appears in the string.

I also have to do this in my program:

Convert strings A and B to all lower case. For each letter in string A, call function on A and B.

This is the code I have so far:

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

char tolower(char c) {

    return c;
}

int countChars(const char* string, char ch)
{
    int count = 0;
    int i;

    int length = strlen(string);

    for (i = 0; i < length; i++)
    {
        if (string[i] == ch)
        {
            count++;
        }
    }
    return count;
}

int main() {
    char stringA[50];
    char stringB[50];

    cout << "Please enter first string: ";
    cin.getline(stringA, 50);

    cout << "Please enter second string: ";
    cin.getline(stringB, 50);

    cout << "String 1: " << stringA << endl;
    cout << "String 2: " << stringB << endl;

    if (strcmp(stringA, stringB) == 0) {
        cout << "These strings are anagrams" << endl;
    }
    else {
        cout << "These strings are not anagrams" << endl;
    }

}

Upvotes: 0

Views: 514

Answers (3)

Loki Astari
Loki Astari

Reputation: 264331

The easiest way to do this is to count the number of each letter. Note you don't need to count every letter just the ones are in the string. To compare them store the results in a map.

#include <iostream>
#include <string>
#include <map>
#include <array>

class AnagramCheck
{
    // I am going to be lazy and use map to store the counts.
    // But you can do this much smarter and get a better complexity
    // by using an array. But that involves more checks
    std::map<char, int> data;
    public:
        explicit AnagramCheck(std::string const& str)
        {
            for(auto x: str) {
                ++data[std::tolower(x)]; // If you want to be smart ignore space/punctuation
            }
        }
        bool operator==(AnagramCheck const& rhs) const
        {
            return data == rhs.data;
        }
};

int main()
{
    std::string         line1;
    std::getline(std::cin, line1);
    AnagramCheck        line1Check(line1);

    std::string         line2;
    std::getline(std::cin, line2);
    AnagramCheck        line2Check(line2);

    if (line1Check == line2Check) {
        std::cout << "Anagram\n";
    }

}

Upvotes: 0

No Name
No Name

Reputation: 98

Just sort the both the strings and then compare it.

sort(stringA, stringA+strlen(stringA)) sort(stringB, stringB+strlen(stringB))

just before the if statement.

You were checking if the 2 strings are exactly equal. But anagrams can have different arrangement of letters but must have same letters.

Though you need to check may need to check if the 2 strings are same (if you don't consider a string to be an anagram of itself).

PS- a google search would have instantly provided you ans.

Upvotes: 0

Rohan Bari
Rohan Bari

Reputation: 7726

If you want to do so in a simple and readable way, consider the following code:

#include <string>
#include <iostream>
#include <algorithm>

bool is_anagram(std::string s1, std::string s2)
{
    std::sort(s1.begin(), s1.end());
    std::sort(s2.begin(), s2.end());
    return s1 == s2;
}

int main(void)
{
    std::string s1, s2;

    std::cout << "Input first string: ";
    getline(std::cin, s1);

    std::cout << "Input second string: ";
    getline(std::cin, s2);

    if (is_anagram(s1, s2))
        std::cout << "Strings are anagrams." << std::endl;
    else
        std::cout << "Strings are NOT anagrams." << std::endl;

    return 0;
}

The function is_anagram() returns true when the sorted strings (used algorithm library for it) are same.

Note: The strings are case-sensitive. Hello and hello are different.

Hope it helps you. :D

Upvotes: 2

Related Questions