#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]!='') { s2[i]=s1[i]; i++; } //since the '' is not copied s2[i]=''; printf("\nCopied String is %s ",s2); getch(); }
Output :
Enter the string : c4learn.com Copied String is c4learn.com
Explanation :
i=0; while(s1[i]!='') { 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]='';
No comments:
Post a Comment