Code: #include <string> #include <iostream> using namespace std; int main(){ string s1="123"; //Set up string 1 string s2="456"; //Set up string 2 string s3; //Setup string 3 cout<<"First string: "<<s1<<endl; cout<<"Second string: "<<s2<<endl; s3=s1; //Copy string 1 into string 3 for(int i=0;i<s2.size();i++){ //Loop through s2 char c=s2[i]; //Grab current character from s2 bool is_c_in_s3=true; for(int j=0;j<s3.size();j++){ //Loop though s3 cout<<"Figure this portion out yourself."<<endl; } if(is_c_in_s3){ s3+=c; //Add the element to String 3 } } cout<<"Final string: "<<s3<<endl; } I need help from you. I try to write the code in C++ which return union of two strings , This is the code, if you know how to write the right code please help me...
There is no function that can return anything and if main returning anything would not solve the issue. No one would do that for you unless you show your effort to do it yourself.
Oh yes, I didn't noticed that it's main function. BTW. you can use function strcat() for concatenating strings together, unless you wanted to do it literally your own way without using any preprogrammed functions.
Hey, good daddy has come to you! I took a closer look on your function and I tried to compile your code. The output was good, it returned "123456". POC: http://postimage.org/image/fuae7joij/
It's pretty easy, where are you stuck? Have you figured out how to do it on paper? Cos if you can't do it yourself, then there's no way you'll be able to program a computer to be able to do it. BTW to the other poster, strcat won't do the trick; the task is to return the *union* of the strings. Think back to set theory. So the union of "123" and "345" is "12345", not "123345" which would be the strcat result.
Hi I bothered to read your program and corrected a few things. I hope that you read my program carefully and learn from it. I hope that it is not your homework. Best regards Chong Code: #include <iostream.h> #include <string> using namespace std; main() { string s1 = "1234", s2 = "456",s3; cout << "First string: "<<s1.c_str()<<'\n'; cout << "Second string: " << s2.c_str() <<'\n'; s3 = s1; 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 cout << "Final string: " << s3.c_str() << '\n'; }//main
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?
By "help you solve it" do you mean "write it for you"? Me: no, but you might be able to get some other mug to do your homework for you. But where are you stuck? What have you tried? You can probably solve this by guessing. Try solving it a few times on paper, and then try describing (to a friend, or a dog, or the wall - whatever works) how you do it, and why you choose whether or not to include a character in the result, and see if you can derive an algorithm from 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 }
Chong, please use the code blocks when posting code snippets in posts. See http://www.go4expert.com/misc.php?do=bbcode#code
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.
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 }