943,538 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1948
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Aug 15th, 2009
0

Re: Anagram Function - Reapeated letters Problem

Well, THis was my approach to the code.

C++ Syntax (Toggle Plain Text)
  1. for (int a=0; a<str1.size();a++)
  2. {
  3. size_t val=str2.find(str1[a]);
  4. if(val==string::npos)
  5. return false;
  6. else
  7. str2.erase(val,val);
  8. }
  9. return str2.empty();
THough i dint try it out, hope it works fine.
Last edited by Sky Diploma; Aug 15th, 2009 at 9:39 am. Reason: Corrected Syntax errors. and some logical errors.,
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
Aug 15th, 2009
1

Re: Anagram Function - Reapeated letters Problem

A Small change would be .....

C++ Syntax (Toggle Plain Text)
  1.  
  2. str2.erase(val,val);
to
C++ Syntax (Toggle Plain Text)
  1. str2.erase(val,1);

Thanks to tux4life.
Last edited by Sky Diploma; Aug 15th, 2009 at 9:58 am.
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
Aug 15th, 2009
0

Re: Anagram Function - Reapeated letters Problem

And....Another approach (maybe not the most efficient one):
C++ Syntax (Toggle Plain Text)
  1. bool isAnagram(string str1, string str2)
  2. {
  3. sort(str1.begin(), str1.end());
  4. sort(str2.begin(), str2.end());
  5. if(str1 == str2) return true;
  6.  
  7. return false;
  8. }
(You'll need to include the algorithm header file, you can do this by adding the following include directive to your program: #include <algorithm> )
Last edited by tux4life; Aug 15th, 2009 at 10:22 am. Reason: add info about include
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Aug 15th, 2009
0

Re: Anagram Function - Reapeated letters Problem

.
Last edited by firstPerson; Aug 15th, 2009 at 11:44 am.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is online now Online
3,862 posts
since Dec 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