C Program to Copy One String into Other Without Using Library Function.
Program : C Program to Copy One String into Other Without Using Library Function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include<stdio.h> int main() { char s1[100], s2[100]; int i; printf("\nEnter the string :"); gets(s1); i = 0; while (s1[i] != '\0') { s2[i] = s1[i]; i++; } s2[i] = '\0'; printf("\nCopied String is %s ", s2); return (0); } |
Output :
1 2 |
Enter the string : c4learn.com Copied String is c4learn.com |
Explanation :
1 2 3 4 5 |
i = 0; while (s1[i] != '\0') { s2[i] = s1[i]; i++; } |
- Scan Entered String From Left to Right , Character by Character.
- In Each Iteration Copy One Character To New String Variable.
- As soon as Source or Original String Ends , Process of Coping Character Stops but we still haven’t Copied NULL Character into new String so , Append Null Character to New String.
1 |
s2[i] = '\0'; |