- 1、本文档共13页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
- 5、该文档为VIP文档,如果想要下载,成为VIP会员后,下载免费。
- 6、成为VIP后,下载本文档将扣除1次下载权益。下载后,不支持退款、换文档。如有疑问请联系我们。
- 7、成为VIP后,您将拥有八大权益,权益包括:VIP文档下载权益、阅读免打扰、文档格式转换、高级专利检索、专属身份标志、高级客服、多端互通、版权登记。
- 8、VIP文档为合作方或网友上传,每下载1次, 网站将根据用户上传文档的质量评分、类型等,对文档贡献者给予高额补贴、流量扶持。如果你也想贡献VIP文档。上传文档
查看更多
C 语言字符串操作
字符串操作 --c 语言
本章集中讨论字符串操作,包括拷贝字符串,拷贝字符串的一部分,比较字符串,字符串右
对齐,删去字符串前后的空格,转换字符串,等等。 C 语言提供了许多用来处理字符串的标
准库函数,本章将介绍其中的一部分函数。
在编写 C 程序时,经常要用到处理字符串的技巧,本章提供的例子将帮助你快速学会一
些常用函数的使用方法,其中的许多例子还能有效地帮助你节省编写程序的时间。
6 .1 串拷贝 (strcpy) 和内存拷贝 (memcpy) 有什么不同 ?它们适合于在哪种情况下使用 ?
strcpy() 函数只能拷贝字符串。 strcpy() 函数将源字符串的每个字节拷贝到目录字符串
中,当遇到字符串末尾的 null 字符 (\0) 时,它会删去该字符,并结束拷贝。
memcpy() 函数可以拷贝任意类型的数据。 因为并不是所有的数据都以 null 字符结束, 所
以你要为 memcpy() 函数指定要拷贝的字节数。
在拷贝字符串时,通常都使用 strcpy() 函数;在拷贝其它数据 ( 例如结构 ) 时,通常都
使用 memcpy()函数。
以下是一个使用 strcpy() 函数和 memcpy() 函数的例子:
#include stdio. h
#include string. h
typedef struct cust-str {
int id ;
char last_name [20] ;
char first_name[l5];
} CUSTREC;
void main (void);
void main (void)
{
char * src_string = This is the source string ;
char dest_string[50];
CUSTREC src_cust;
CUSTREC dest_cust;
printf(Hello! Im going to copy src_string into dest_string!\n);
/ * Copy src_ string into dest-string. Notice that the destination
string is the first argument. Notice also that the strcpy()
function returns a pointer to the destination string. * /
printf(Done! dest_string is: %s\n ,
strcpy(dest_string, src_string)) ;
printf(Encore! Lets copy one CUSTREC to another. \n) ;
prinft(Ill copy src_cust into dest_cust. \n);
/ * First, intialize the src_cust data members. * /
src_cust. id = 1 ;
strcpy(src_cust. last_name, Strahan);
strcpy(src_cust. first_name, Troy);
/ * Now, Use the memcpy() function to copy the src-cust structure to
the dest_cust structure. Notice that, just as with strcpy(), the
destination comes first. * /
memcpy(dest_cust, src_cust, sizeof(CUSTREC));
printf(Done! I just
文档评论(0)