Anagram Function - Reapeated letters Problem

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2009
Posts: 147
Reputation: gretty is an unknown quantity at this point 
Solved Threads: 7
gretty gretty is offline Offline
Junior Poster

Anagram Function - Reapeated letters Problem

 
0
  #1
Aug 14th, 2009
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:
  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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Anagram Function - Reapeated letters Problem

 
0
  #2
Aug 14th, 2009
Letter frequency counter?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 147
Reputation: gretty is an unknown quantity at this point 
Solved Threads: 7
gretty gretty is offline Offline
Junior Poster

Re: Anagram Function - Reapeated letters Problem

 
0
  #3
Aug 14th, 2009
Originally Posted by iamthwee View Post
Letter frequency counter?
I am not quite what you mean?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Anagram Function - Reapeated letters Problem

 
0
  #4
Aug 14th, 2009
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.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 675
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 99
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: Anagram Function - Reapeated letters Problem

 
0
  #5
Aug 14th, 2009
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.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,244
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 154
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso

Re: Anagram Function - Reapeated letters Problem

 
0
  #6
Aug 14th, 2009
Let the string class do all the work. use stringVariable.find() function.

Here is an example :
  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. }
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,244
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 154
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso

Re: Anagram Function - Reapeated letters Problem

 
0
  #7
Aug 14th, 2009
Correction : Its
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 147
Reputation: gretty is an unknown quantity at this point 
Solved Threads: 7
gretty gretty is offline Offline
Junior Poster

Re: Anagram Function - Reapeated letters Problem

 
0
  #8
Aug 15th, 2009
Originally Posted by firstPerson View Post
Let the string class do all the work. use stringVariable.find() function.

Here is an example :
  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?
if(!s1[0])
return false; // is this checking if string one if greater than 0 characters?
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,244
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 154
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso

Re: Anagram Function - Reapeated letters Problem

 
0
  #9
Aug 15th, 2009
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.
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 44
Reputation: zalezog is an unknown quantity at this point 
Solved Threads: 11
zalezog zalezog is offline Offline
Light Poster

Re: Anagram Function - Reapeated letters Problem

 
0
  #10
Aug 15th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC