use variables to compare strings

I am trying to compare two strings and need some help please.

Lets say string one is str1 and derived from a cin statement
cin >> str1;

str2 is done the same cin >> str2;

now I would like to take the first character of str1 and see if it exists in str2, if it does then store it in say used1 if not store it in unused1.

repeat for each character in str1. In the end have a list of charachters that are in both str1 and str2 and a list of char that are in only in str1.

Thanks in advance,
string has a find() method.

1
2
3
string hw = "Hello World";
if( hw.find( 'e' ) )
   cout << "Found an e!" << endl;


Repeat above for each character in str1 (checking to see if it exists in str2), then repeat for each character in str2 (checking to see if it exists in str1).

There is only one subtle problem -- to build used1, you will find all the common characters twice -- once during the first loop and once during the second.


jsmith,

thanks for the quick reply, however I won't know what str1 is so can i pass to hw.find the index of str1 iterating through the string for beginning to end.



for (int i=0; i < str1.size(); ++i);
if ( hw.find(str1[i]) )
etc
not sure if that is clear or not
That will work.
Yup, you are on the right track!
Topic archived. No new replies allowed.