# union of two strings

• 02-11-2012
miki
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
• 02-11-2012
Salem
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.
• 02-11-2012
grumpy
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.
• 02-11-2012
miki
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.
• 02-11-2012
rogster001
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.
• 02-11-2012
Elysia
You need to make a flowchart.