I need a code which will print union of two strings. for example
string1="1,2,3"
string2="3,4,5"

and print the result string3: 1,2,3,4,5

Could someone help me how to solve this?

Depends on the type of string. For STL string objects you can use the + or the += operator. For C style strings you can use strcat(). If this is a school project and you can't use any of the standard functions/methods, then you can use a nested loop with direct assignment of elements from one string to the other (and don't forget o to put the null terminating char at the end of the string, if needed).

Yes it is my school homework, and please if you know help me how to solve this task. I can't use functions.

Could give me some example how to write this?

you need to solve your hw to learn and it is not an advanced question.
but we can tell you how to think gust tell us what is the string1 data type

Ok I accept your suggestions, 2 string's type is char and the result should be char. any help from you will be very useful for me, thanks

You are using the wrong data structure. You should use a vector, array, set or some other container than a string. With your example strings (which elements are characters) your union would be ",12345" .
If you must use a string you are going to have to break it up into its constituent parts first.

ok
first :let us say each string length = 7 so the max result is 15
Ex:
s1=1,2,3,4
s2=5,6,7,8
s3=1,2,3,4,5,6,7,8
second :
to union we will use the naive algorithm
1- copy open string into result
2-for each element in second string if it is not in the first one add to result

Could you help how to write the the algorithm with code..

Read this code, then fix it.

#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;
}

Please post your variable declarations:

char string1[] or
char * string1 or
string string1

Note that all three objects above could have multiple elements, each of which could be accessed using the [] operator. Each element of a string is a char object. There is a default assignment operator built into the language so to assign one char to another when both are in arrays you can go:

type x[MAX]
type y[MAX]
x[a] = y;

where type would be cha, MAX could be any expression that resolves to a constant integer and a and b need not, but may be, the same value.

#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);
}

Can somebody help me to remove the errors, i try something to do. this function should make union of two strings thanks

Read this code, then fix it.

#include <string>
#include <iostream>
using namespace std;
int main(){
    string s1="123"; //Set up string 1
    string s2="356"; //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;
}

Your code really works but I have one more problem, it prints the result 123356 and the number 3 twice, but there is a problem because code should print just once. thanks a lot for your help

Your code really works but I have one more problem, it prints the result 123356 and the number 3 twice, but there is a problem because code should print just once. thanks a lot for your help

Did you happen to address the

cout<<"Figure this portion out yourself."<<endl;

portion of that example?

What is your level of effort here?

Read this code, then fix it.

#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

the result 123456

Read this code, then fix it.

#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

result 123456

#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

Can somebody help me to remove the errors, i try something to do. this function should make union of two strings thanks

the result is abcv

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
	s3=s1; // copy s1 into s3
	for(i=0;i<s2.size();i++) // 
	{
		int index=s1.find(s2[i]);
		if(index<0)
		{
			s3.append(",",1);
			s3.append(s2.data()+i,1);
		}
	}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.