收藏 分销(赏)

C字符串处理函数全.doc

上传人:綻放 文档编号:8916111 上传时间:2025-03-07 格式:DOC 页数:3 大小:482.18KB 下载积分:8 金币
下载 相关 举报
C字符串处理函数全.doc_第1页
第1页 / 共3页
C字符串处理函数全.doc_第2页
第2页 / 共3页


点击查看更多>>
资源描述
strcpy(char destination[], const char source[]); strcpy:将字符串 source 拷贝到字符串 destination 中。 strcpy 函数应用举例 原型:strcpy(char destination[], const char source[]); 功能:将字符串 source 拷贝到字符串 destination 中 例程: #include <iostream.h> #include <string.h> void main(void) { char str1[10] = { "TsinghuaOK"}; char str2[10] = { "Computer"}; cout <<strcpy(str1,str2)<<endl; } 运行结果是:Computer 第二个字符串将覆盖掉第一个字符串的所有内容! 注意:在定义数组时,字符数组 1 的字符串长度必须大于或等于字符串 2 的字符串长度。不 能用赋值语句将一个字符串常量或字符数组直接赋给一个字符数组。 所有字符串处理函数都 包含在头文件 string.h 中。 strncpy(char destination[], const char source[], int numchars); strncpy:将字符串 source 中前 numchars 个字符拷贝到字符串 destination 中。 strncpy 函数应用举例 原型:strncpy(char destination[], const char source[], int numchars); 功能:将字符串 source 中前 numchars 个字符拷贝到字符串 destination 中 例程: #include <iostream.h> #include <string.h> void main(void) { char str1[10] = { "Tsinghua "}; char str2[10] = { "Computer"}; cout <<strncpy(str1,str2,3)<<endl; } 运行结果:Comnghua 注意: 字符串 source 中前 numchars 个字符将覆盖掉字符串 destination 中前 numchars 个字符! strcat(char target[], const char source[]); strcat:将字符串 source 接到字符串 target 的后面。 strcat 函数应用举例 原型:strcat(char target[], const char source[]); 功能:将字符串 source 接到字符串 target 的后面 例程: #include <iostream.h> #include <string.h> void main(void) { char str1[] = { "Tsinghua "}; char str2[] = { "Computer"}; cout <<strcpy(str1,str2)<<endl; } 运行结果:Tsinghua Computer 注意:在定义字符数组 1 的长度时应该考虑字符数组 2 的长度,因为连接后新字符串的长度 为两个字符串长度之和。进行字符串连接后,字符串 1 的结尾符将自动被去掉,在结尾串末 尾保留新字符串后面一个结尾符。 strncat(char target[], const char source[], int numchars); strncat:将字符串 source 的前 numchars 个字符接到字符串 target 的后面。 strncat 函数应用举例: 原型:strncat(char target[], const char source[], int numchars); 功能:将字符串 source 的前 numchars 个字符接到字符串 target 的后面 例程: #include <iostream.h> #include <string.h> void main(void) { char str1[] = { "Tsinghua "}; char str2[] = { "Computer"}; cout <<strncat(str1,str2,3)<<endl; } 运行结果:Tsinghua Com int strcmp(const char firststring[], const char secondstring); strcmp:比较两个字符串 firststring 和 secondstring。 strcmp 函数应用举例 原型:int strcmp(const char firststring[], const char secondstring); 功能:比较两个字符串 firststring 和 secondstring 例程: #include <iostream.h> #include <string.h> void main(void) { char buf1[] = "aaa"; char buf2[] = "bbb"; char buf3[] = "ccc"; int ptr; ptr = strcmp(buf2,buf1); if(ptr > 0) cout <<"Buffer 2 is greater than buffer 1"<<endl; else cout <<"Buffer 2 is less than buffer 1"<<endl; ptr = strcmp(buf2,buf3); if(ptr > 0) cout <<"Buffer 2 is greater than buffer 3"<<endl; else cout <<"Buffer 2 is less than buffer 3"<<endl; } 运行结果是:Buffer 2 is less than buffer 1 Buffer 2 is greater than buffer 3 strlen( const char string[] ); strlen:统计字符串 string 中字符的个数。 strlen 函数应用举例 原型:strlen( const char string[] ); 功能:统计字符串 string 中字符的个数 例程: #include <iostream.h> #include <string.h> void main(void) { char str[100]; cout <<"请输入一个字符串:"; cin >>str; cout <<"The length of the string is :"<<strlen(str)<<"个"<<endl;
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 教育专区 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服