function which returns union of two strings

Discussion in 'C++' started by zemzela, Feb 11, 2012.

  1. zemzela

    zemzela New Member

    Joined:
    Feb 11, 2012
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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...
     
  2. Scripting

    Scripting John Hoder

    Joined:
    Jun 29, 2010
    Messages:
    421
    Likes Received:
    57
    Trophy Points:
    0
    Occupation:
    School for life
    Location:
    /root
    This function doesnt return anything, you must use "return" .
     
  3. zemzela

    zemzela New Member

    Joined:
    Feb 11, 2012
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Could you fix code, it is really important for me. thank you for your time
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    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.
     
  5. Scripting

    Scripting John Hoder

    Joined:
    Jun 29, 2010
    Messages:
    421
    Likes Received:
    57
    Trophy Points:
    0
    Occupation:
    School for life
    Location:
    /root
    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.
     
  6. Scripting

    Scripting John Hoder

    Joined:
    Jun 29, 2010
    Messages:
    421
    Likes Received:
    57
    Trophy Points:
    0
    Occupation:
    School for life
    Location:
    /root
    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/
     
  7. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     
  8. Chong

    Chong New Member

    Joined:
    May 15, 2011
    Messages:
    29
    Likes Received:
    7
    Trophy Points:
    0
    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 a moderator: Feb 13, 2012
  9. zemzela

    zemzela New Member

    Joined:
    Feb 11, 2012
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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?
     
  10. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     
  11. Chong

    Chong New Member

    Joined:
    May 15, 2011
    Messages:
    29
    Likes Received:
    7
    Trophy Points:
    0
    +++++++++++++++++++++++++++
    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 a moderator: Feb 18, 2012
  12. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  13. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     
  14. Chong

    Chong New Member

    Joined:
    May 15, 2011
    Messages:
    29
    Likes Received:
    7
    Trophy Points:
    0
    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 a moderator: Feb 25, 2012

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice