belongs to collection: String User Defined Function Examples/Programs
In this program we are implementing our own strcpy() function, here we will learn how to copy one string to another without using library function?
In this program, we will read a string and copy the string into another using stringCopy() function which is implemented by own.
#include <stdio.h> /******************************************************** * function name :stringCpy * Parameter :s1,s2 : string * Description : copies string s2 into s1 ********************************************************/ void stringCpy(char* s1,char* s2); int main() { char str1[100],str2[100]; printf("Enter string 1: "); scanf("%[^\n]s",str1);//read string with spaces stringCpy(str2,str1); printf("String 1: %s \nString 2: %s\n",str1,str2); return 0; } /******** function definition *******/ void stringCpy(char* s1,char* s2) { int i=0; while(s2[i]!='\0') { s1[i]=s2[i]; i++; } s1[i]='\0'; /*string terminates by NULL*/ }
Output
Enter string 1: Help in Programming String 1: Help in Programming String 2: Help in Programming
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
program to copy one string to another (implementation of strcpy) in C
Output
need an explanation for this answer? contact us directly to get an explanation for this answer