Problem statement
Write a program to copy one string into another string using user defined function.
Here don’t use standard string handling function strcpy().
Program to copy one string into another string without using strlen()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include<stdio.h> void ustrcpy(char*,char*); int main() { char sstr[20] = "WebEncyclop"; char tstr[20]; ustrcpy(tstr,sstr); printf("source string is %s\n",sstr); printf("target string is %s", tstr); } void ustrcpy(char*t, char*s) { while(*s != '\0' ) { *t = *s; t++; s++; } *t = '\0'; } |
Output
1 2 |
source string is WebEncyclop target string is WebEncyclop |
In this program we write our own function ustrcpy() which is called in main().
To ustrcpy we pass address of first element of strings.
Using while loop we copy each character into target string from source string.
at the last we put ‘\0’ which indicates end of string.