| | |
Anagram Function - Reapeated letters Problem
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2009
Posts: 147
Reputation:
Solved Threads: 7
Hello
I have made a function that determines whether 2 words are an anagram or not.
My Problem is: that if the word contains 2 or more of the same letters(eg hello, arrest, gall) the function doesn't work even when the 2 words input are an anagram,
If i input "ate" & "eat" my function returns true - so it works
But if I input "mello" & "ollem" - I know these are not words but it should still return true, but it doesn't because the repeated 'l' confuses the function...I think?
function:
Whole code:
I have made a function that determines whether 2 words are an anagram or not.
My Problem is: that if the word contains 2 or more of the same letters(eg hello, arrest, gall) the function doesn't work even when the 2 words input are an anagram,
If i input "ate" & "eat" my function returns true - so it works
But if I input "mello" & "ollem" - I know these are not words but it should still return true, but it doesn't because the repeated 'l' confuses the function...I think?

function:
C++ Syntax (Toggle Plain Text)
bool areAnagrams (string s1, string s2) // pre : s1 and s2 are lower case strings // post : returns true if s1 and s1 are anagrams // and otherwise false { int count = 0; if (s1.length() != s2.length()) { return false; } // check if all the letters that occur in s1 are // present in s2 also. for (int i=0; i<(int)s1.length(); i++) { for (int j=0; j<(int)s1.length(); j++) { if (s1[i]==s2[j]) count++; } } if (count==(int)s1.length()) { return true; } else return false; }
Whole code:
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; bool areAnagrams(string s1, string s2); int main() { string word1, word2; cout << "Enter pairs of words - terminate with control-z" << endl; while (cin >> word1 >> word2) // complete this { if (areAnagrams (word1, word2)) cout << word1 << " and " << word2 << " are anagrams " << endl; else cout << word1 << " and " << word2 << " are not anagrams " << endl; } system("pause"); return 0; } bool areAnagrams (string s1, string s2) // pre : s1 and s2 are lower case strings // post : returns true if s1 and s1 are anagrams // and otherwise false { int count = 0; if (s1.length() != s2.length()) { return false; } // check if all the letters that occur in s1 are // present in s2 also. for (int i=0; i<(int)s1.length(); i++) { for (int j=0; j<(int)s1.length(); j++) { if (s1[i]==s2[j]) count++; } } if (count==(int)s1.length()) { return true; } else return false; }
Last edited by gretty; Aug 14th, 2009 at 4:03 am.
Hey a better way would be to delete the particular letter in the string after you encounter it.
using the
In the end you will have to just chek if the string is empty(); and then you have a anagram.
using the
string::erase() function.In the end you will have to just chek if the string is empty(); and then you have a anagram.
Let the string class do all the work. use stringVariable.find() function.
Here is an example :
Here is an example :
C++ Syntax (Toggle Plain Text)
bool isAnagram(string& str1, string& str2) { //check for length equality if(str1.length() != str2.length()) return false; if(!str1) return false; //check for string equality if(str1 == str2) return true; for(int i = 0; i < str1.length(); i++) { //search both ways if(str1.find(str2[i]) == string::npos) return false; if(str2.find(str1[i]) == string::npos) return false; } return true; }
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e, Paul Thompson] 2) What does this sequence equal to : (.5u - .5a)(.5u-.5b)(.5u-.5c) ... 3) What is the 123456789 prime numer? Ask4Answer
Correction : Its
if(!str1[0])
return false;
and not
if(!str1)
return false;
if(!str1[0])
return false;
and not
if(!str1)
return false;
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e, Paul Thompson] 2) What does this sequence equal to : (.5u - .5a)(.5u-.5b)(.5u-.5c) ... 3) What is the 123456789 prime numer? Ask4Answer
•
•
Join Date: Apr 2009
Posts: 147
Reputation:
Solved Threads: 7
•
•
•
•
Let the string class do all the work. use stringVariable.find() function.
Here is an example :
C++ Syntax (Toggle Plain Text)
bool isAnagram(string& str1, string& str2) { //check for length equality if(str1.length() != str2.length()) return false; if(!str1) return false; //check for string equality if(str1 == str2) return true; for(int i = 0; i < str1.length(); i++) { //search both ways if(str1.find(str2[i]) == string::npos) return false; if(str2.find(str1[i]) == string::npos) return false; } return true; }

One question: what does this line do? Am I right in saying that it checks that s1 is larger than 0 characters?
•
•
•
•
if(!s1[0])
return false; // is this checking if string one if greater than 0 characters?
It checks to see if there is a string to check for anagram.
Since technically this check below :
if(str1.length() != str2.length())
return false;
could pass if both string is null;
So it checks to see if they are null;
Also I only need to check 1 string for null, since it will already
be determined that they both have the same length.
Since technically this check below :
if(str1.length() != str2.length())
return false;
could pass if both string is null;
So it checks to see if they are null;
Also I only need to check 1 string for null, since it will already
be determined that they both have the same length.
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e, Paul Thompson] 2) What does this sequence equal to : (.5u - .5a)(.5u-.5b)(.5u-.5c) ... 3) What is the 123456789 prime numer? Ask4Answer
•
•
Join Date: Oct 2008
Posts: 44
Reputation:
Solved Threads: 11
I didn't want to poke but firstPerson's method doesn't take into account the frequency of characters, so for inputs
like s1 = roorsseett
s2 = sosretetet
returns true.
letter s1 s2
r 2 1
o 2 1
.
.
or inputs like
s1 = rorre
s2 = oroee
returns true.
Another method would be to sort the strings(may be using quicksort) and then check character by character, if both the strings are anagrams, then after sorting both s1[index] must be equal to s2[index] else strings are not anagrams.
Another thing to take into account are whether there are spaces, and to strip them before sorting.While capitalized characters should be converted into lower case before sorting.
You'll also have to decide whether to take into characters other than alphanumeric ones. Consider
s1 = O, Draconian Devil
s2 = Leonardo Da Vinci
you have got to remove that comma in any method you follow as well as the spaces
like s1 = roorsseett
s2 = sosretetet
returns true.
letter s1 s2
r 2 1
o 2 1
.
.
or inputs like
s1 = rorre
s2 = oroee
returns true.
Another method would be to sort the strings(may be using quicksort) and then check character by character, if both the strings are anagrams, then after sorting both s1[index] must be equal to s2[index] else strings are not anagrams.
Another thing to take into account are whether there are spaces, and to strip them before sorting.While capitalized characters should be converted into lower case before sorting.
You'll also have to decide whether to take into characters other than alphanumeric ones. Consider
s1 = O, Draconian Devil
s2 = Leonardo Da Vinci
you have got to remove that comma in any method you follow as well as the spaces
Last edited by zalezog; Aug 15th, 2009 at 8:49 am.
![]() |
Similar Threads
- Sony Vaio Fn key problem (USB Devices and other Peripherals)
- tolower function does not work on certain letters (C++)
- problem with calling a function (C++)
- problem in sqlcxt() function in pro*c (C++)
- operator/function for a list of letters (Python)
- Big Problem, Generic Error (VB.NET)
- Create Function that prints letters, words and sentences (C++)
Other Threads in the C++ Forum
- Previous Thread: inheritence problem with more explanation
- Next Thread: write to file program
| Thread Tools | Search this Thread |
api array arrays based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion convert count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






