Copy string without built-in function

 #include <stdio.h>
 int main()
 {
   char s1[100], s2[100], i;
   printf("Enter string s1: ");
   scanf("%s",s1);
   for(i = 0; s1[i] != '\0'; i++)
   {
      s2[i] = s1[i];
   }
   s2[i] = '\0';
   printf("String s2: %s", s2);
   return 0;
 }
Explaination of the Program : Here, we declared two variables of data type char which can store two strings. In one string, we are going to enter the string which the users will input. And then we will use for loop, the condition will be till the null character of string isn't identified. Then inside the loop, we are copy the data from one string to another string and the loop gets terminated when the null character is identified. Now we are adding the null character to string two and then printing it's value.
Previous
Next Post »
0 Comment