stract strcpy strlen strcmp函数

这几个函数都是字符串处理函数所以头文件必须包含string.h

1.     字符串连接函数stract()

#include<stdio.h>

#include<string.h>

void main()

{

       chara[80]="Your name is ";

       charb[80]="zui shuai de dzy";

       strcat(a,b);

       puts(a);

}

运行可得

2.     字符串比较函数 strcpy,字符串不是常数,无法进行直接比较所以用这个函数可知两个字符串是否一致如

#include<stdio.h>

#include<string.h>

void main()

{

       chara[80]="Your name is ";

       charb[80]="Your name is ";

       if(strcmp(a,b)==0)

       printf("1\n");

       elseprintf("2\n");

}

这个函数必须好好说说,因为它的返回值是个骗子,它的返回值规律为:要是比较的两个字符串相等则返回0否则返回一个非0的数这样的话就不能单纯的用if表判断了,因为if是若是为0则为假就不执行为非0才能执行,所以虽说字符比较函数能比较出俩个字符串是否相等但必须要用if的话则需和0做比较例如上边的代码为正确的而下边代码则和我们的意思“两个字符串相等则输出1,否则输出2”正好相反

#include<stdio.h>

#include<string.h>

void main()

{

       chara[80]="Your name is ";

       charb[80]="Your name is ";

       if(strcmp(a,b))

       printf("1\n");

       elseprintf("2\n");

}

3.     字符串复制函数strcpy(),没啥好说的就是后边的复制到前边

4.     测量字符串长度的函数strlen,输出的不包括\0也就是我们看到的字符长度。如

#include<stdio.h>

#include<string.h>

void main()

{

       chara[80]="Your name is ";

       charb[80]="dzy ";

 

printf("%d\n",strlen(a));

 

 

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值