Google Add

Search

C Program to Copy One String to Another Without Using Any Library Function


What is String


In simple words string is an array of characters.


Let's explain this


char c;

c is a character which takes only one value.

Now declare this


char c[100];

In this declaration c is an array of characters, which take upto 100 characters. 



CProgram to Copy One String to Another Without Using Any Library Function


In c strcpy is used to copy the contents of one string into another. But here we copy one string contents into other string without using strcpy function.



#include <stdio.h>
#include <string.h>

main()
{
 
 char str1[100],str2[100];
 int i=0;
 
 printf("Enter string");
 gets(str1);

   /* While string is not reach at the end*/
   while(str1[i]!='\0'){

    str2[i]=str1[i];
    i++;

  }
 
 printf("string copied is %s",str2);
}


Output:

Enter String : Cquestions.in

String copied is : Cquestions.in

No comments:

Post a Comment