Chong's Avatar, Join Date: May 2011
Go4Expert Member
Quote:
Originally Posted by zemzela View Post
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
}

Last edited by shabbir; 18Feb2012 at 13:56.. Reason: Code blocks
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Chong, please use the code blocks when posting code snippets in posts. See http://www.go4expert.com/misc.php?do=bbcode#code
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
OK, Chong, if reading completed code samples is the best way to learn, then why don't professional teachers do it that way?

The fact is you learn programming by doing it yourself, not by looking at completed code samples. It's too easy to fool yourself after looking at code written by someone else to say "oh that looks easy, I could have done that" - the fact is you COULDN'T do that.

What will the OP do in his exam when he doesn't have you to write his code for him? That might make it easy for him outside exam conditions, but he'll be stuffed when he's in a situation where he has no internet access.
Chong's Avatar, Join Date: May 2011
Go4Expert Member
Hi Z.
I believe in making thigns easy for beginners. When I was a begfinner, I had chances to read many well writtne programs of other programmers. I hope that you gtet it too. I have been accused of being a mug. I don't mind it.
***********************************
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
}

Last edited by shabbir; 25Feb2012 at 13:56.. Reason: Code blocks