Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   function which returns union of two strings (http://www.go4expert.com/forums/function-returns-union-strings-t27775/)

zemzela 11Feb2012 21:51

function which returns union of two strings
 
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...

Scripting 12Feb2012 03:18

Re: function which returns union of two strings
 
This function doesnt return anything, you must use "return" .

zemzela 12Feb2012 03:24

Re: function which returns union of two strings
 
Could you fix code, it is really important for me. thank you for your time

shabbir 12Feb2012 08:34

Re: function which returns union of two strings
 
Quote:

Originally Posted by Scripting (Post 92232)
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 (Post 92233)
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.

Scripting 12Feb2012 18:52

Re: function which returns union of two strings
 
Quote:

Originally Posted by shabbir (Post 92236)
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.

Scripting 12Feb2012 20:50

Re: function which returns union of two strings
 
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/

xpi0t0s 13Feb2012 04:52

Re: function which returns union of two strings
 
Quote:

Originally Posted by zemzela (Post 92233)
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.

Chong 13Feb2012 14:37

Re: function which returns union of two strings
 
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


zemzela 18Feb2012 00:59

Re: function which returns union of two strings
 
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?

xpi0t0s 18Feb2012 06:11

Re: function which returns union of two strings
 
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.


All times are GMT +5.5. The time now is 17:21.