C code for copying one string to other

strCpy:C code for user defined function to copy one string to another
#include<stdio.h>
void strCpy(char *p2, char *p1);
int main()
{
char str1[20];
char str2[20];

printf("Enter a string:\n");
gets(str1);

strCpy(str2,str1);
printf("Copied string is:%s\n",str1);
return 0;
}

void strCpy(char *p2, char *p1)
{
while(*p1!='\0')
{
*p2=*p1;
p2++;
p1++;
}
*p2='\0';
}
/*
OUTPUT:
-bash-3.2$ gcc strCpy.c
/tmp/ccaP29bY.o: In function `main':
strCpy.c:(.text+0x24): warning: the `gets' function is dangerous and should not be used.
-bash-3.2$ ./a.out
Enter a string:
india
Copied string is:india
-bash-3.2$ ./a.out
Enter a string:
hello india
Copied string is:hello india
-bash-3.2$

*/

No comments:

Post a Comment