C Program To Concatenate Two Strings
Learn How To Concatenate Two Strings in C Programming Language. Learn how to concatenate strings without strcat() Library function as well as using Pointers.
A String is basically an Array of Characters indicated by ‘\0’ as its terminating character.
Method 1: C Program Code To Concatenate Two Strings using strcat() Library Function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include<stdio.h> #include<string.h> int main() { char string_a[20]; char string_b[20]; printf("Enter the First String:\t"); scanf("%s", string_a); printf("\nEnter the Second String:\t"); scanf("%s", string_b); strcat(string_a, string_b); printf("\nFirst String:\t%s\n", string_a); printf("\nSecond String:\t%s", string_b); printf("\n"); return 0; } |
Method 2: C Program To Concatenate A String without using strcat() and with Pointers
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 29 30 31 | #include<stdio.h> void strcatfunc(char *, char *); int main() { char string_a[20], string_b[20]; printf("\nEnter the First String:\t"); scanf("%s", string_a); printf("\nEnter the Second String:\t"); scanf("%s", string_b); strcatfunc(string_a, string_b); printf("\nFirst String:\t%s\n", string_a); printf("\nSecond String:\t%s\n", string_b); return 0; } void strcatfunc(char *ptr1, char *ptr2) { while(*ptr1 != '\0') { ptr1++; } while(*ptr2 != '\0') { *ptr1 = *ptr2; ptr1++; ptr2++; } *ptr1 = '\0'; } |
Output

If you have any compilation errors or doubts in this C Program To Concatenate Strings using Pointers, let us know about in the Comment Section below.
Is Strings important for interviews? Or should I only focus on Pointers and Data Structures?