C Language Examples - String Copy

C Language Examples

Copy the string of one variable to another variable.

Example - Using strcpy()

#include <stdio.h> #include <string.h> int main() { char src[40]; char dest[100]; memset(dest, '\0', sizeof(dest)); strcpy(src, "This is example.com"); strcpy(dest, src); printf("The final target string: %s\n", dest); return(0); }

Output result:

最终的目标字符串: This is example.com

Example - Without using strcpy()

#include <stdio.h> int main() { char s1[100], s2[100], i; printf("String s1:"); scanf("%s",s1); for(i = 0; s1[i] != '\0'; ++i) { s2[i] = s1[i]; } s2[i] = '\0'; printf("String s2: %s", s2); return 0; }

Output result:

字符串 s1: example
字符串 s2: example

C Language Examples

other extensions