#include <string.h> char *strcpy(char *to, const char *from);
strcpy
copies characters from the second argument string, from
,
to the first argument string, to
, until a terminating-null
character is found. The null also is copied.
to
string.
to
string for all the characters of the from
string. Characters are
copied until a null character is found, or until a protection or
addressing exception occurs.
The effect of strcpy
is not defined if the to
and from
fields overlap.
<string.h>
is included (by an #include
statement) and strcpy
is not undefined (by an #undef
statement), strcpy
is implemented by inline code.
#include <string.h> #include <stdio.h> #include <stdlib.h> #define WORDSIZE 20 main() { char *text = "Some of this "line" is in "quotes"."; char *quotepos1, *quotepos2; char word[WORDSIZE]; printf("The input text is:n%sn", text); quotepos1 = strchr(text, '"'); if (quotepos1 == NULL) { puts("There are no quotes in this line."); abort(); } quotepos2 = strchr(quotepos1+1, '"'); if (quotepos2 == NULL) { puts("There is only one quotation mark in this line."); abort(); } if (quotepos2 - quotepos1 > WORDSIZE) puts("The first word in quotes is too large to handle."); else { strcpy(word, quotepos1+1); /* Copy the word. */ word[quotepos2-quotepos1-1] = '0'; /* Null-terminate the word. */ printf("The first word in quotation marks in the text is "%s".", word); } }
memcpy
, strcat
, strncpy
, strsave
Copyright (c) 1998 SAS Institute Inc. Cary, NC, USA. All rights reserved.