function which returns union of two strings

Newbie Member
11Feb2012,21:51   #1
zemzela's Avatar
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...
John Hoder
12Feb2012,03:18   #2
Scripting's Avatar
This function doesnt return anything, you must use "return" .
Newbie Member
12Feb2012,03:24   #3
zemzela's Avatar
Could you fix code, it is really important for me. thank you for your time
Go4Expert Founder
12Feb2012,08:34   #4
shabbir's Avatar
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.
John Hoder
12Feb2012,18:52   #5
Scripting's Avatar
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.
John Hoder
12Feb2012,20:50   #6
Scripting's Avatar
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/
Mentor
13Feb2012,04:52   #7
xpi0t0s's Avatar
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.
Go4Expert Member
13Feb2012,14:37   #8
Chong's Avatar
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
Newbie Member
18Feb2012,00:59   #9
zemzela's Avatar
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?
Mentor
18Feb2012,06:11   #10
xpi0t0s's Avatar
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.