943,607 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1948
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 14th, 2009
0

Anagram Function - Reapeated letters Problem

Expand Post »
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:
C++ Syntax (Toggle Plain Text)
  1. bool areAnagrams (string s1, string s2)
  2. // pre : s1 and s2 are lower case strings
  3. // post : returns true if s1 and s1 are anagrams
  4. // and otherwise false
  5. {
  6. int count = 0;
  7.  
  8. if (s1.length() != s2.length()) {
  9. return false;
  10. }
  11.  
  12. // check if all the letters that occur in s1 are
  13. // present in s2 also.
  14.  
  15. for (int i=0; i<(int)s1.length(); i++) {
  16. for (int j=0; j<(int)s1.length(); j++) {
  17. if (s1[i]==s2[j]) count++;
  18. }
  19. }
  20.  
  21. if (count==(int)s1.length())
  22. {
  23. return true;
  24. }
  25. else return false;
  26.  
  27. }

Whole code:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. bool areAnagrams(string s1, string s2);
  5. int main()
  6. {
  7. string word1, word2;
  8. cout << "Enter pairs of words - terminate with control-z" << endl;
  9.  
  10. while (cin >> word1 >> word2) // complete this
  11. {
  12. if (areAnagrams (word1, word2))
  13. cout << word1 << " and " << word2 << " are anagrams " << endl;
  14. else
  15. cout << word1 << " and " << word2 << " are not anagrams " << endl;
  16. }
  17. system("pause");
  18. return 0;
  19. }
  20. bool areAnagrams (string s1, string s2)
  21. // pre : s1 and s2 are lower case strings
  22. // post : returns true if s1 and s1 are anagrams
  23. // and otherwise false
  24. {
  25. int count = 0;
  26.  
  27. if (s1.length() != s2.length()) {
  28. return false;
  29. }
  30.  
  31. // check if all the letters that occur in s1 are
  32. // present in s2 also.
  33.  
  34. for (int i=0; i<(int)s1.length(); i++) {
  35. for (int j=0; j<(int)s1.length(); j++) {
  36. if (s1[i]==s2[j]) count++;
  37. }
  38. }
  39.  
  40. if (count==(int)s1.length())
  41. {
  42. return true;
  43. }
  44. else return false;
  45.  
  46. }
Last edited by gretty; Aug 14th, 2009 at 4:03 am.
Similar Threads
Reputation Points: 10
Solved Threads: 7
Junior Poster
gretty is offline Offline
158 posts
since Apr 2009
Aug 14th, 2009
0

Re: Anagram Function - Reapeated letters Problem

Letter frequency counter?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Aug 14th, 2009
0

Re: Anagram Function - Reapeated letters Problem

Click to Expand / Collapse  Quote originally posted by iamthwee ...
Letter frequency counter?
I am not quite what you mean?
Reputation Points: 10
Solved Threads: 7
Junior Poster
gretty is offline Offline
158 posts
since Apr 2009
Aug 14th, 2009
0

Re: Anagram Function - Reapeated letters Problem

Just do a match on the letters

for example hello has:

1 h
1 e
1 o
2 l

Therefore any word which has the same will be an anagram.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Aug 14th, 2009
0

Re: Anagram Function - Reapeated letters Problem

Hey a better way would be to delete the particular letter in the string after you encounter it.

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.
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
Aug 14th, 2009
0

Re: Anagram Function - Reapeated letters Problem

Let the string class do all the work. use stringVariable.find() function.

Here is an example :
C++ Syntax (Toggle Plain Text)
  1. bool isAnagram(string& str1, string& str2)
  2. {
  3. //check for length equality
  4. if(str1.length() != str2.length())
  5. return false;
  6.  
  7. if(!str1)
  8. return false;
  9.  
  10. //check for string equality
  11. if(str1 == str2) return true;
  12.  
  13. for(int i = 0; i < str1.length(); i++)
  14. {
  15. //search both ways
  16.  
  17. if(str1.find(str2[i]) == string::npos)
  18. return false;
  19. if(str2.find(str1[i]) == string::npos)
  20. return false;
  21. }
  22.  
  23. return true;
  24. }
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Aug 14th, 2009
0

Re: Anagram Function - Reapeated letters Problem

Correction : Its
if(!str1[0])
return false;

and not
if(!str1)
return false;
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Aug 15th, 2009
0

Re: Anagram Function - Reapeated letters Problem

Let the string class do all the work. use stringVariable.find() function.

Here is an example :
C++ Syntax (Toggle Plain Text)
  1. bool isAnagram(string& str1, string& str2)
  2. {
  3. //check for length equality
  4. if(str1.length() != str2.length())
  5. return false;
  6.  
  7. if(!str1)
  8. return false;
  9.  
  10. //check for string equality
  11. if(str1 == str2) return true;
  12.  
  13. for(int i = 0; i < str1.length(); i++)
  14. {
  15. //search both ways
  16.  
  17. if(str1.find(str2[i]) == string::npos)
  18. return false;
  19. if(str2.find(str1[i]) == string::npos)
  20. return false;
  21. }
  22.  
  23. return true;
  24. }
Thanks for the example, I never knew about the string find function & that npos aswell

One question: what does this line do? Am I right in saying that it checks that s1 is larger than 0 characters?
Quote ...
if(!s1[0])
return false; // is this checking if string one if greater than 0 characters?
Reputation Points: 10
Solved Threads: 7
Junior Poster
gretty is offline Offline
158 posts
since Apr 2009
Aug 15th, 2009
0

Re: Anagram Function - Reapeated letters Problem

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.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Aug 15th, 2009
0

Re: Anagram Function - Reapeated letters Problem

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
Last edited by zalezog; Aug 15th, 2009 at 8:49 am.
Reputation Points: 53
Solved Threads: 13
Light Poster
zalezog is offline Offline
47 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: inheritence problem with more explanation
Next Thread in C++ Forum Timeline: write to file program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC