C++ anagram code. Help please

justinoh

Member
Nov 15, 2001
134
0
0
Can someone check my code please? I did a little extra testing and tried "anagrammatize" and "anagramatizez". The code returned the answer as an anagram when it shouldn't be. Where is my mistake in the code

// returns true if number of times each character appears in both strings are the same
int check_for_anagram(string str1, string str2)
{
int ch1, ch2;
for (int i = 0; i < str1.size(); i++)
{

char ch = str1.at(i);
if (ch == str1.at(i++))
ch1++;
}
for (int j = 0; j < str2.size(); j++)
{

char ch = str2.at(j);
if (ch == str2.at(j++))
ch2++;
}
if (ch1 == ch2)
return true;
}

Message edited
 

Haircut

Platinum Member
Apr 23, 2000
2,248
0
0
You have declared ch1 and ch2 within their respective for loops, so they are not visible outside the loops.
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
I don't see how your code could possibly work. Care to explain your logic behind the code?
 

justinoh

Member
Nov 15, 2001
134
0
0
I'll post the whole code here:


#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

// returns true if number of times each character appears in both strings are the same
int check_for_anagram(string str1, string str2)
{
int ch1, ch2;
for (int i = 0; i < str1.size(); i++) //keeps looping until all characters in string have been tested
{

char ch = str1.at(i);
if (ch == str1.at(i++)) //if characters are the same, adds 1 to it
ch1++;
}
for (int j = 0; j < str2.size(); j++)
{

char ch = str2.at(j);
if (ch == str2.at(j++))
ch2++;
}
if (ch1 == ch2)
return true;
}

// returns true if the strings are the same length
bool sameLength(string str1, string str2)
{
int sizeStr1, sizeStr2;
sizeStr1 = str1.size();
sizeStr2 = str2.size();
if (sizeStr1 == sizeStr2)
return true;
}

int main ()
{
string str1, str2;
cout << "Enter the first string: ";
cin >> str1;
cout << "Enter the second string: ";
cin >> str2;
if (check_for_anagram(str1, str2) && sameLength(str1, str2))
cout << str2 << " is an anagram of " << str1 << "." << endl;
else
cout << str2 << " is not an anagram of " << str1 << "." << endl;
system ("Pause");
return 0;
}

I might have made a mistake in check_for_anagram(string str1, string str2). It should return true only if the number of occurences for each character are the same for each string. Thus far, i'm not sure how to fix it. I'm still fairly new at this and may have presumed something incorrectly. If someone can help me I would greatly appreciate it.

Thanks in advance!
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Let me make a couple of recommendations :)

1. You should check the size of the strings first. If they're not equal, then no anagram exists. Your function check_for_anagram does not need to do anything if the size doesn't match. You can also check for size equality by simply doing str1.size() == str2.size(). Don't know why you're making it tougher.


2. Think about your logic in checking for an anagram. I don't think you have the 'algorithm' figured out. Try to do a few examples on paper... abab & baba, aaaa & bbbb etc.


Edit: Didn't read the whole code
 

glugglug

Diamond Member
Jun 9, 2002
5,340
1
81
That logic is pretty flawed. Its almost counting repeated chars within each word but not quite. Which has almost nothing to do with them being anagrams anyway. I suggest starting from scratch.

I would suggest the following alternate algorithm:

Treat the 2 strings as STL vectors (which they basically are).
SORT the elements of them each separately.
Then just compare the strings.