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));

}

猜你喜欢

转载自blog.csdn.net/dy1314fowever/article/details/80355624
今日推荐