About Me

My photo
Raipur, Chhattisgarh, India
Hi , I am Amit Thakur. I have worked as a QA Engineer for two years and as a Java Developer for one year in NIHILENT TECHNOLOGIES PVT. LTD., Pune.Currently I am working as DEAN (Research & Development) in Bhilai Institute of Technology, Raipur.

Wednesday, September 4, 2013

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]!='')
 {
  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++;
 }
  1. Scan Entered String From Left to Right , Character by Character.
  2. In Each Iteration Copy One Character To New String Variable.
  3. 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: