Concat two strings without built-in function

#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);
   for(i = 0; s1[i] != '\0'; ++i);
   for(j = 0; s2[j] != '\0'; ++j, ++i)
   {
      s1[i] = s2[j];
   }
   s1[i] = '\0';
   printf("After concatenation: %s", s1);
   return 0;
 }
Explaination Of the Program : Here, we are declaring two char data type variables which can store two strings. We are accepting two strings and storing them in s1 and s2. Now in the first for loop, the condition is that for loop will work it the s1's null character isn't identified. Once, it is identified the loop will get terminated. The first for loop only gets the i counter to the end of the first string. Now in the second for loop, we are going to copy the data 
of the s2 in s1 and the data will get copied at the end of the s1 because the counter has reached there. Adding the null character after the copying is done and then we are printing the string s1 which concates the data of s2 inside s1.
Previous
Next Post »
0 Comment