Finding the union of two strings

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

Join Date: Jun 2006
Posts: 16
Reputation: hay_man is an unknown quantity at this point 
Solved Threads: 0
hay_man hay_man is offline Offline
Newbie Poster

Finding the union of two strings

 
1
  #1
Sep 19th, 2006
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:
  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,618
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1491
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Finding the union of two strings

 
0
  #2
Sep 19th, 2006
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".
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 16
Reputation: hay_man is an unknown quantity at this point 
Solved Threads: 0
hay_man hay_man is offline Offline
Newbie Poster

Re: Finding the union of two strings

 
1
  #3
Sep 19th, 2006
How does one check each char at a time? As you can see, i've only had success at comparing a whole string. Thanks
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,618
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1491
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Finding the union of two strings

 
0
  #4
Sep 19th, 2006
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.
  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. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 16
Reputation: hay_man is an unknown quantity at this point 
Solved Threads: 0
hay_man hay_man is offline Offline
Newbie Poster

Re: Finding the union of two strings

 
1
  #5
Sep 19th, 2006
Fantastic, gr8 help Ancient Dragon. After 8hrs of sitting behind this screen your saviour was lifeline!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 148
Reputation: Micko is on a distinguished road 
Solved Threads: 6
Micko Micko is offline Offline
Junior Poster

Re: Finding the union of two strings

 
1
  #6
Sep 21st, 2006
You should also look at this
Last edited by Micko; Sep 21st, 2006 at 2:17 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 16
Reputation: hay_man is an unknown quantity at this point 
Solved Threads: 0
hay_man hay_man is offline Offline
Newbie Poster

Re: Finding the union of two strings

 
0
  #7
Sep 21st, 2006
nicko, thats my post. thanks anywayy
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,618
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1491
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Finding the union of two strings

 
0
  #8
Sep 21st, 2006
Originally Posted by hay_man View Post
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:
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 16
Reputation: hay_man is an unknown quantity at this point 
Solved Threads: 0
hay_man hay_man is offline Offline
Newbie Poster

Re: Finding the union of two strings

 
1
  #9
Sep 21st, 2006
because that is dealing wih strings i want to adapt it to handle vectors
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,618
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1491
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Finding the union of two strings

 
0
  #10
Sep 21st, 2006
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.
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC