union of two strings

This is a discussion on union of two strings within the C++ Programming forums, part of the General Programming Boards category; Can someone help me how to write a code for union of two strings. for example first string1="mon", second string2="mok" ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    2

    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,489
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,255
    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%.

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    2

    union of two strings

    Code:
    #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
    
    
        }
        for(j=0;j<s3.size();j++)
        {
         if(c!=s3[j]) // compare c with current value in s3
         {
              s3=s3+c; // conncatenate c to s3;
         }
        
         i++; 
         j++;
    
    
        }
        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
    Location
    Liverpool UK
    Posts
    1,422
    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."

    Stroustrup:
    "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
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    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, 01:46 AM
  2. Union -Need Ur Help
    By ganesh bala in forum C Programming
    Replies: 5
    Last Post: 01-28-2009, 03: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, 12: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21