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
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
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.
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.
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.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); }
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:FLTK: "The most fun you can have with your clothes on.""Are you sure your sanity chip is fully screwed in sir?" (Kryten)
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'"