C program to concatenate or join to string; Through this tutorial, we will learn how to concatenate or join two string using for loop, while loop, function and pointer in c program.
Programs and Algorithm to Concatenate Two Strings in C
Let’s use the following algorithm and program to concatenate or join two string using for loop, while loop, function and pointer in c:
- Algorithm To Concatenate Two Strings
- C program to Concatenate Two Strings using For Loop
- C program to Concatenate Two Strings While Loop
- C program to Concatenate Two Strings using Function
- C program to Concatenate Two Strings uisng Pointer
Algorithm To Concatenate Two Strings
Use the following algorithm to write a program concatenate or join two string; as follows:
- START
- Step 1 -> Take two string as str1 and str2.
- Step 2 -> Move to the last position of first string let it be i.
- Step 3 -> Now Loop over second string with j = 0
- Assign the character str1[i] = str2[j]
- Increment i and j and repeat till the last element of j.
- End Loop
- Step 3 -> Print str1.
- STOP
C program to Concatenate Two Strings using For Loop
/* C program to Concatenate Two Strings without using strcat() */ #include <stdio.h> #include <string.h> int main() { char Str1[100], Str2[100]; int i, j; printf("\n Please Enter the First String : "); gets(Str1); printf("\n Please Enter the Second String : "); gets(Str2); // To iterate First String from Start to end for (i = 0; Str1[i]!='\0'; i++); // Concatenating Str2 into Str1 for (j = 0; Str2[j]!='\0'; j++, i++) { Str1[i] = Str2[j]; } Str1[i] = '\0'; printf("\n String after the Concatenate = %s", Str1); return 0; }
The output of the above c program; as follows:
Please Enter the First String : hello Please Enter the Second String : world String after the Concatenate = helloworld
C program to Concatenate Two Strings While Loop
#include <stdio.h> #include <string.h> int main() { char Str1[100], Str2[100]; int i, j; printf("\n Please Enter the First String : "); gets(Str1); printf("\n Please Enter the Second String : "); gets(Str2); i = 0; while( Str1[i]!='\0') { i++; } j = 0; while( Str2[j]!='\0') { Str1[i] = Str2[j]; i++; j++; } Str1[i] = '\0'; printf("\n String after the Concatenate = %s", Str1); return 0; }
The output of the above c program; as follows:
Please Enter the First String : hello Please Enter the Second String : dear String after the Concatenate = hellodear
C program to Concatenate Two Strings using Function
#include <stdio.h> #include <string.h> void concatenate(char [], char []); int main() { char Str1[100], Str2[100]; printf("\n Please Enter the First String : "); gets(Str1); printf("\n Please Enter the Second String : "); gets(Str2); concatenate(Str1, Str2); printf("\n String after the Concatenate = %s", Str1); return 0; } void concatenate(char s1[], char s2[]) { int i, j; i = 0; while( s1[i]!='\0') { i++; } j = 0; while( s2[j]!='\0') { s1[i] = s2[j]; i++; j++; } s1[i] = '\0'; }
The output of the above c program; as follows:
Please Enter the First String : hello Please Enter the Second String : world String after the Concatenate = helloworld
C program to Concatenate Two Strings uisng Pointer
#include <stdio.h> #include <string.h> int main() { char Str1[100], Str2[100]; char *s1 = Str1; char *s2 = Str2; printf("\n Please Enter the First String : "); gets(Str1); printf("\n Please Enter the Second String : "); gets(Str2); while(* ++s1); while(*(s1++) = (*s2++)); printf("\n String after the Concatenate = %s", Str1); return 0; }
The output of the above c program; as follows:
Please Enter the First String : hello Please Enter the Second String : world String after the Concatenate = helloworld