Thread: union of two strings

  1. #1
    Registered User
    Join Date
    Feb 2012

    union of two strings

    Can someone help me how to write a code for union of two strings. for example first string1="mon", second string2="mok" and the result of the trhird string should be "monk". I need this code in c++. thank a lot

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Announcements - C++ Programming

    And what have you tried?

    You need to show some effort, as this shows us
    a) that you're not a lazy sponger hoping to score free homework
    b) what you're capable of doing yourself
    c) what you're actually stuck on.

    The last bit is important, it means we can give you a specific direct answer to the specific problem you're stuck on.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2005
    First step: Describe, on paper, a set of instructions of how you would work out what the union of two strings is. Use words that are as simple and unambiguous as possible. Test if those instructions work by seeing if, by following them literally and pedantically, you can get the result you intend. If you can't, then refine those instructions until you can.

    If you can't do that, then you can't hope to write code (in C++, or any other programming language) to do it.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Feb 2012

    union of two strings

    #include <string>
    #include <iostream>
    using namespace std;
    string s1="a,b,c"; //Set up string 1
    string s2="b,v"; //Set up string 2
    string s3; //Setup string 3
    int i=0; //init counters
    int j=0;
    char c;
    string un(string s1,string s2)  //function for union of two strings
      s3=s1; // copy s1 into s3
        for(i=0;i<s2.size();i++) // 
         c=s2[i]; // value from the current position in c
         if(c!=s3[j]) // compare c with current value in s3
              s3=s3+c; // conncatenate c to s3;
        return (s3);
    i tried something to do but i really need your help. if somebody have time to look through my code, please help. thank a lot.

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Liverpool UK
    You are still not giving any useful detail in your query, but anyway your variable declarations are mostly floating around outside of your function, are they supposed to be global? If not it wont compile because they are not in scope, but the algorithm doesnt work anyhow. So what is the problem you face right now? the algorithm does not work? or it wont compile? If i assume you have ' int main()' below this function with a call to un() then its probably that your output is wrong??

    You see its all guesswork because you are not saying enough to let people give you better help.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  6. #6
    Join Date
    Oct 2007
    Inside my computer
    You need to make a flowchart.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Union of a set
    By bman900 in forum C Programming
    Replies: 5
    Last Post: 11-07-2010, 02:46 AM
  2. Union -Need Ur Help
    By ganesh bala in forum C Programming
    Replies: 5
    Last Post: 01-28-2009, 04:57 AM
  3. union
    By studentc in forum C Programming
    Replies: 2
    Last Post: 05-13-2004, 01:37 PM
  4. union
    By shiju in forum C Programming
    Replies: 2
    Last Post: 01-22-2004, 01:52 AM
  5. if union could...
    By black in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2002, 08:34 AM

Tags for this Thread