How to Concatenate Two Strings
Here you will know that How to Concatenate Two Strings in CPP with and without using strcat function.
strcat() function is a built-in C++ library function that concatenates (joins) two strings together.
Program Code to Concatenate Two Strings without strcat()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #include<iostream> #include<stdio.h> using namespace std; int main() { char string1[50], string2[50], string3[100]; int i, j=0; cout<<"Enter the First String: "; gets(string1); cout<<"Enter the Second String: "; gets(string2); for(i=0; string1[i]!='\0'; i++) { string3[i]=string1[i]; j++; } for(i=0; string2[i]!='\0'; i++) { string3[j]=string2[i]; j++; } string3[j] = '\0'; cout<<"\nString after Concatenation: "<<string3; cout<<endl; return 0; } |
Output
Enter the First String: Code
Enter the Second String: Revise
String after Concatenation: CodeRevise
Program Code to Concatenate Two Strings with strcat()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include<iostream> #include<stdio.h> #include<string.h> using namespace std; int main() { char string1[50], string2[50], string3[100]; int i, j=0; cout<<"Enter the First String: "; gets(string1); cout<<"Enter the Second String: "; gets(string2); strcat(string1,string2); cout<<"\nString after Concatenation: "<<string1; cout<<endl; return 0; } |
Output
Enter the First String: Code
Enter the Second String: Revise
String after Concatenation: CodeRevise
Check out our other C++ programming Examples