944,134 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5468
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 19th, 2006
1

Finding the union of two strings

Expand Post »
I am programming in C++ and am attempting to find the intersection of two strings, only the items common to both and thus return a third string with these items. Essentially, I am trying to write
the code for the STL library set_intersection.
To approach this problem I thought of I can use two nested loops one loop to cycle thru each string. Then having a variable to compare values in common, which means I would
have to have a temp variable to store common elements.
something similar to this:
C++ Syntax (Toggle Plain Text)
  1. string names[] = {"AMC", "ANZ", "AMX"};
  2. for( int i = 0; i < 3; i++ ) {
  3. for( int j = 0; j < 3; j++ ) {
  4. cout << names[i].compare(names[j] ) << " ";
  5. }
  6. cout << endl;
  7. }
Any further sugestions would be great,
Thanks,
I
Similar Threads
Reputation Points: 38
Solved Threads: 0
Newbie Poster
hay_man is offline Offline
16 posts
since Jun 2006
Sep 19th, 2006
0

Re: Finding the union of two strings

the compare() function is not what you need. It only tells you whether one string is the same as the other. you need to check the two strings one character at a time and create a third string that contains the characters which are in both strings. For example, the first two strings have the letters 'A' and 'M' in both strings, so a third string will contain "AM".
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is online now Online
21,963 posts
since Aug 2005
Sep 19th, 2006
1

Re: Finding the union of two strings

How does one check each char at a time? As you can see, i've only had success at comparing a whole string. Thanks
Reputation Points: 38
Solved Threads: 0
Newbie Poster
hay_man is offline Offline
16 posts
since Jun 2006
Sep 19th, 2006
0

Re: Finding the union of two strings

you can use a loop. Something like below. The find() method will search one string for an instance of another string and return the position where its at.
C++ Syntax (Toggle Plain Text)
  1. string str1 = "ABC";
  2. string str2 = "BCD";
  3. string result;
  4.  
  5. for(int i = 0; i < str2.length(); ++)
  6. {
  7. if( str1.find( str2[i] ) != string::npos )
  8. result += str2[i];
  9. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is online now Online
21,963 posts
since Aug 2005
Sep 19th, 2006
1

Re: Finding the union of two strings

Fantastic, gr8 help Ancient Dragon. After 8hrs of sitting behind this screen your saviour was lifeline!
Reputation Points: 38
Solved Threads: 0
Newbie Poster
hay_man is offline Offline
16 posts
since Jun 2006
Sep 21st, 2006
1

Re: Finding the union of two strings

You should also look at this
Last edited by Micko; Sep 21st, 2006 at 2:17 am.
Reputation Points: 55
Solved Threads: 6
Junior Poster
Micko is offline Offline
148 posts
since Aug 2005
Sep 21st, 2006
0

Re: Finding the union of two strings

nicko, thats my post. thanks anywayy
Reputation Points: 38
Solved Threads: 0
Newbie Poster
hay_man is offline Offline
16 posts
since Jun 2006
Sep 21st, 2006
0

Re: Finding the union of two strings

Click to Expand / Collapse  Quote originally posted by hay_man ...
nicko, thats my post. thanks anywayy
Your solution was nearly identicale to mine except your made a check to insure duplicates are not inserted into the new string. If you already knew the solution why did you post the question in this thread?:eek: :rolleyes:
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is online now Online
21,963 posts
since Aug 2005
Sep 21st, 2006
1

Re: Finding the union of two strings

because that is dealing wih strings i want to adapt it to handle vectors
Reputation Points: 38
Solved Threads: 0
Newbie Poster
hay_man is offline Offline
16 posts
since Jun 2006
Sep 21st, 2006
0

Re: Finding the union of two strings

now you are asking a completly different question. vectors of what? strings? do you want to create a vector that is a union of two other vectors, where each string in the new vector is the union of the corresponding strings in two or more other vectors? The algorithm would be almost identical to what has already been posted.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is online now Online
21,963 posts
since Aug 2005

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: Buffering
Next Thread in C++ Forum Timeline: adding a '\' to a CString object





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


Follow us on Twitter


© 2011 DaniWeb® LLC