Quote:
Originally Posted by zemzela
This code works, but does not work for this example:
string1 = " rry"
string2 = " yoh"
result is string3 =" rryoh"
The result should be ryoh, not rryoh. This code does not check different letters from first string, just for second.
Can you help me how to solve that?
Hi Zemzela
I am having difficulty in accessing this forum to edit. Here as below I have my own version of the program. I have been accused of bing a mug. I don't mind it. I believe in making things easy for beginners. When I was a beginner, I luckly had and read many well written programs of other programmers. I hope that you get it too.
Best regards
Chong
++++++++++++++++++
Code:
#include <iostream.h>
#include <string>
using namespace std;
void Union(string &a,string &b);
int main()
{
string s1 = "122347", s2 = "44567",s3="";
cout << "First string: "<<s1.c_str()<<'\n';
cout << "Second string: " << s2.c_str() <<'\n';
Union(s1,s3);
Union(s2,s3);
cout << "Final string: " << s3.c_str() << '\n';
return 0;
}//main
void Union(string &s2,string &s3)
{
for (int i=0;i<s2.size();i++){
char c = s2[i];
bool is_c_in_s3 =false;
for (int j=0;j<s3.size();j++){
if (c==s3[j]){
is_c_in_s3 = true;
break;
}
}//for
if (!is_c_in_s3) s3+=c;
}//for
}



