function which returns union of two strings

zemzela's Avatar
Newbie Member
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...
0
Scripting's Avatar, Join Date: Jun 2010
John Hoder
This function doesnt return anything, you must use "return" .
0
zemzela's Avatar
Newbie Member
Could you fix code, it is really important for me. thank you for your time
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by Scripting View Post
This function doesnt return anything, you must use "return" .
There is no function that can return anything and if main returning anything would not solve the issue.

Quote:
Originally Posted by zemzela View Post
Could you fix code, it is really important for me. thank you for your time
No one would do that for you unless you show your effort to do it yourself.
0
Scripting's Avatar, Join Date: Jun 2010
John Hoder
Quote:
Originally Posted by shabbir View Post
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.
0
Scripting's Avatar, Join Date: Jun 2010
John Hoder
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/
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Quote:
Originally Posted by zemzela View Post
Could you fix code, it is really important for me. thank you for your time
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.
0
Chong's Avatar, Join Date: May 2011
Go4Expert Member
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

Last edited by shabbir; 13Feb2012 at 15:05.. Reason: Code blocks
0
zemzela's Avatar
Newbie Member
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?
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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.