C program to Concatenate Two Strings without using strcat(): We perform concatenation of two string programmatically without any inbuilt function.
String: string is a collection of characteC program to Concatenate Two Strings without using strcat()rs and string always ends with null character ‘ \0’.

(Note: it is necessary to append null character at end of the string which indicates the ends of string)

Algorithm to concatenate two strings

  1. Declare two strings. str1 and str2
  2. Take input two strings
  3. Traverse first string till end
  4. Traverse second string
    If str2[chr] != ‘\0’
    Add each character of str2 to str1
  5. Print first string (concatenated string)

C program to Concatenate Two Strings without using strcat()

#include <stdio.h>
int main()
{
char s1[100], s2[100], i, j;

printf("Enter first string: ");
scanf("%s", s1);

printf("Enter second string: ");
scanf("%s", s2);

// calculate the length of string s1
// and store it in i
for(i = 0; s1[i] != '
#include <stdio.h>
int main()
{
char s1[100], s2[100], i, j;

printf("Enter first string: ");
scanf("%s", s1);

printf("Enter second string: ");
scanf("%s", s2);

// calculate the length of string s1
// and store it in i
for(i = 0; s1[i] != '\0'; ++i);

for(j = 0; s2[j] != '\0'; ++j, ++i)
{
s1[i] = s2[j];
}

s1[i] = '\0';
printf("i =",i);
printf("concatenated string = : %s", s1);

return 0;
}
'; ++i); for(j = 0; s2[j] != '
#include <stdio.h>
int main()
{
char s1[100], s2[100], i, j;

printf("Enter first string: ");
scanf("%s", s1);

printf("Enter second string: ");
scanf("%s", s2);

// calculate the length of string s1
// and store it in i
for(i = 0; s1[i] != '\0'; ++i);

for(j = 0; s2[j] != '\0'; ++j, ++i)
{
s1[i] = s2[j];
}

s1[i] = '\0';
printf("i =",i);
printf("concatenated string = : %s", s1);

return 0;
}
'; ++j, ++i) { s1[i] = s2[j]; } s1[i] = '
#include <stdio.h>
int main()
{
char s1[100], s2[100], i, j;

printf("Enter first string: ");
scanf("%s", s1);

printf("Enter second string: ");
scanf("%s", s2);

// calculate the length of string s1
// and store it in i
for(i = 0; s1[i] != '\0'; ++i);

for(j = 0; s2[j] != '\0'; ++j, ++i)
{
s1[i] = s2[j];
}

s1[i] = '\0';
printf("i =",i);
printf("concatenated string = : %s", s1);

return 0;
}
'; printf("i =",i); printf("concatenated string = : %s", s1); return 0; }

Output:-

C program to Concatenate Two Strings without using strcat()

Write A Comment