Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
C++ program to perform string concatenation:
#include<iostream>
#include<cstring>
using namespace std;
class sstring{
char* s;
int len;
public:
sstring();
sstring(char *a);
void concat(sstring x,sstring y);
void display();
};
sstring::sstring(){
len=0;
s=new char[len+1];
}
sstring::sstring(char *a){
len=strlen(a);
s=new char[len+1];
strcpy(s,a);
}
void sstring::concat(sstring x,sstring y){
len=x.len+y.len;
s=new char[len+1];
strcpy(s,x.s);
strcat(s," ");
strcat(s,y.s);
}
void sstring::display(){
cout<<"\nThe string is "<<s<<endl;
}
int main()
{
char x[100];
cout<<"Enter the first string ";
cin>>x;
sstring a(x);
cout<<"\nEnter the second string ";
cin>>x;
sstring b(x);
sstring c;
c.concat(a,b);
c.display();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.