Anagram program .

Hi I am trying an Anagram program in c++.

Anagram : Two words are said to be anagrams of each other if the letters from one word can be rearranged to form the other word. From the above definition it is clear that two strings are anagrams if all characters in both strings occur same number of times. For example "abc" and "cab" are anagram strings, here every character 'a', 'b' and 'c' occur only one time in both strings. Our algorithm tries to find how many times characters appears in the strings and then comparing their corresponding counts.


This is my Program which doesn't work :

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{

int length;
int count = 0;


vector<string> word1;
vector<string> word2;

string worda;
string wordb;


cout<<"Enter the First word:"<<endl;
cin>>worda;
word1.push_back(worda);
cout<<"Enter the Second Word:"<<endl;
cin>>wordb;
word2.push_back(wordb);







if(worda.size() != wordb.size())
{
cout<<"Not Anagrams"<<endl;
return 0;
}

length = word1.size();

for(int i = 0;i < length;i++)
{

for(int j = 0;j < length; j++)
{

if(word1[i] == word2[j])
{
count++;
}
}
}

if(count == length)
{

cout<<"Anagrams"<<endl;
}
else
cout<<"Not Anagrams"<<endl;




}
Last edited on
Just wondering why you have vectors with 1 word in them - why not just deal with the string?

length = word1.size();

That is the size of the vector not the string.

Edit: Can you please use code tags - the <> button on the right under format.

There is some confused logic in the for loops.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <string>
#include <cctype>
#include <algorithm>

std::string make_key( std::string str )
{
    // remove non-alpha characters
    str.erase( std::remove_if( str.begin(), str.end(),
                    [] ( char c ) { return !std::isalpha(c) ; } ), str.end() ) ;

    for( char& c : str ) c = std::toupper(c) ; // convert to all upper case
    std::sort( str.begin(), str.end() ) ; // and sort

    return str ;
}

inline bool anagrams( const std::string& a, const std::string& b )
{ return make_key(a) == make_key(b) ; }

I had exactly the same problem: why doesn't the code work? Can anyone explain that?

(the code again)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    int length;
    int count = 0;

    vector<string> word1;
    vector<string> word2;

    string worda;
    string wordb;

    cout << "Enter the First word:" << endl;
    cin>> worda;
    word1.push_back(worda);
    cout << "Enter the Second Word:" << endl;
    cin >> wordb;
    word2.push_back(wordb);

    if(worda.size() != wordb.size())
    {
        cout << "Not Anagrams" << endl;
        return 0;
    }

    length = word1.size();

    for(int i = 0;i < length;i++)
    {
         for(int j = 0;j < length; j++)
        {
            if(word1[i] == word2[j])
            {
                count++;
            }
        }
    }

    if(count == length)
    {
        cout << "Anagrams" << endl;
    }
    else cout << "Not Anagrams" << endl;

}
First of all there is no any sence to compare two vectors that contain one element.

Here after the statement

length = word1.size();

length will be equal to 1 because each vector contains only one element

Here

if(word1[i] == word2[j])
{
count++;
}

in fact worda is compared with wordb ant they can be unequal though at the same time be anagrams.

I think you were going to compare characters of the both words not the words themselves.
But in any case the algorithm is invalid. Consider two words

"AAA"
and
"ABC"

If you will compare characters from the first word with characters from the second word then count will be equal to 3. HHowever this does not mean that the words are anagrams.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <string>
#include <cctype>
#include <set>

std::multiset<char> make_key( const std::string& str )
{
    std::multiset<char> key ;
    for( char c : str ) if( std::isalpha(c) ) key.insert( std::toupper(c) ) ;
    return key ;
}

inline bool anagrams( const std::string& a, const std::string& b )
{ return make_key(a) == make_key(b) ; }
Another example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <map>
#include <cctype>
#include <string>

std::map<char, size_t> key_word( const std::string &s )
{
   std::map<char, size_t> m;

   for ( auto &c : s ) if ( std::isalpha( c ) ) ++m[std::toupper( c )];

   return ( m );
}

inline bool IsAnagram( const std::string &s1, const std::string &s2 )  
{
   return ( key_word( s1 ) == key_word( s2 ) );
}
Last edited on

Hi, This program works :

#include <stdio.h>
#include <iostream>

using namespace std;

int main()
{

char ch1[20];
char ch2[20];
int count = 0;

cout<<"enter string 1:"<<endl;
gets(ch1);

cout<<"enter string 2:"<<endl;
gets(ch2);

int length1 = strlen(ch1);

int length2 = strlen(ch2);

if(length1 == length2)
{
for(int i = 0;i < length1;i++)
{
for(int j = 0;j < length1;j++)
{
if(ch1[i] == ch2[j])
{
count++;
break;
}
}
}

if(count == length1)
cout<<"Anagrams"<<endl;
else
cout<<"Not Anagrams"<<endl;
}
else
cout<<"Not Anagrams"<<endl;






system("pause");
}


Topic archived. No new replies allowed.