Program : C Program to Copy One String into Other Without Using Library Function.
#include<stdio.h> #include<conio.h> void main() { char s1[100],s2[100]; int i; //reading a string and finding its length printf("\nEnter the string :"); gets(s1); i=0; while(s1[i]!='\0') { s2[i]=s1[i]; i++; } //since the '\0' is not copied s2[i]='\0'; printf("\nCopied String is %s ",s2); getch(); }
Output :
Enter the string : c4learn.com Copied String is c4learn.com
Explanation :
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.
s2[i]='\0';
Incoming search terms:
- write a simle c program of copy the string in to variables with out using strcpy (1)
- program to copy the string (1)
- program to copy one string to another without using library function in c (1)
- program to copy a string to another without using library function in c (1)
- program to copy a string in c without using string functions (1)
- program to copy a string in c (1)