C++ program to concatenate two strings. Here I given five program to concatenate two string like using strcat() function ,without using strcat() function, using pointers ,using recursion, using operator overloading.
C++ program to concatenate two strings without using strcat() function
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
int main()
{
char str1[20],str2[20];
int lenght,i;
cout<<"Enter two String : ";
cin>>str1>>str2;
lenght=strlen(str1);
for(i=lenght;str2[i-lenght]!='\0';i++)
str1[i]=str2[i-lenght];
str1[i]='\0';
cout<<"After concatenating : "<<str1;
return 0;
}
#include<conio.h>
#include<string.h>
using namespace std;
int main()
{
char str1[20],str2[20];
int lenght,i;
cout<<"Enter two String : ";
cin>>str1>>str2;
lenght=strlen(str1);
for(i=lenght;str2[i-lenght]!='\0';i++)
str1[i]=str2[i-lenght];
str1[i]='\0';
cout<<"After concatenating : "<<str1;
return 0;
}
C++ program to concatenate two strings using strcat() function
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
int main()
{
char str1[20],str2[20];
cout<<"Enter two String : ";
cin>>str1>>str2;
strcat(str1,str2);
cout<<"After concatenating : "<<str1;
return 0;
}
#include<conio.h>
#include<string.h>
using namespace std;
int main()
{
char str1[20],str2[20];
cout<<"Enter two String : ";
cin>>str1>>str2;
strcat(str1,str2);
cout<<"After concatenating : "<<str1;
return 0;
}
C++ program to concatenate two strings using pointers
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char str1[20],str2[20],*p,*q;
cout<<"Enter two string : ";
cin>>str1>>str2;
p=str2;
q=&str1[strlen(str1)];
while(*p!='\0')
{
*q=*p;
q++,p++;
}
*q='\0';
cout<<"After concatenating string : "<<str1;
return 0;
}
#include<string.h>
using namespace std;
int main()
{
char str1[20],str2[20],*p,*q;
cout<<"Enter two string : ";
cin>>str1>>str2;
p=str2;
q=&str1[strlen(str1)];
while(*p!='\0')
{
*q=*p;
q++,p++;
}
*q='\0';
cout<<"After concatenating string : "<<str1;
return 0;
}
C++ program to concatenate two strings using recursion
#include<iostream>
#include<string.h>
using namespace std;
void catstr(char *str1,char *str2,int length,int i)
{
if(str2[i-length]==0)
{
str1[i]='\0';
return;
}
str1[i]=str2[i-length];
catstr(str1,str2,length,i+1);
}
int main()
{
char str1[20],str2[20];
int length,i;
cout<<"Enter two String : ";
cin>>str1>>str2;
length=strlen(str1);
catstr(str1,str2,length,length);
cout<<"After concatenating : "<<str1;
return 0;
}
#include<string.h>
using namespace std;
void catstr(char *str1,char *str2,int length,int i)
{
if(str2[i-length]==0)
{
str1[i]='\0';
return;
}
str1[i]=str2[i-length];
catstr(str1,str2,length,i+1);
}
int main()
{
char str1[20],str2[20];
int length,i;
cout<<"Enter two String : ";
cin>>str1>>str2;
length=strlen(str1);
catstr(str1,str2,length,length);
cout<<"After concatenating : "<<str1;
return 0;
}
C++ program to concatenate two strings using operator overloading
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
struct cat
{
char str[20];
};
cat operator+(cat a,cat b)
{
int i,lenght=strlen(a.str);
for(i=lenght;b.str[i-lenght]!='\0';i++)
a.str[i]=b.str[i-lenght];
a.str[i]='\0';
return a;
}
int main()
{
cat a,b;
int lenght,i;
cout<<"Enter two String : ";
cin>>a.str>>b.str;
a=a+b;
cout<<"After concatenating : "<<a.str;
return 0;
}
#include<conio.h>
#include<string.h>
using namespace std;
struct cat
{
char str[20];
};
cat operator+(cat a,cat b)
{
int i,lenght=strlen(a.str);
for(i=lenght;b.str[i-lenght]!='\0';i++)
a.str[i]=b.str[i-lenght];
a.str[i]='\0';
return a;
}
int main()
{
cat a,b;
int lenght,i;
cout<<"Enter two String : ";
cin>>a.str>>b.str;
a=a+b;
cout<<"After concatenating : "<<a.str;
return 0;
}
No comments:
Post a Comment