1.strlen
功能:计算字符串的长度
#include <stdio.h>
#include <string.h>
int main()
{
int len;
char name[]="sunwu kong";
len = strlen(name);
printf("%d",len);
return 0;
}
结果:10,没有换行符,算空格;
2.strcpy
功能:拷贝一个字符串到另一个字符串
#include <stdio.h>
#include <string.h>
int main()
{
char name[]="sunwukong";
char ch[20];
strcpy(ch,name);
printf("%s",ch);
return 0;
}
结果:sunwukong
3.strcat
把二个字符串拼起来
#include<stdio.h>
#include <string.h>
int main()
{
char ai[20],ma[20];
strcpy(ai,"The is one,");
strcpy(ma,"the is two");
strcat(ai,ma);
printf("%s",ai);
return 0;
}
结果:aima
4.strcmp
功能:比较两个字符串是否一样,比较的是ASCII 值
#include<stdio.h>
#include <string.h>
int main()
{
char text[50],copy[50];
strcpy(text,"The is one,");
strcpy(copy,"the is two");
if( strcmp(text,copy)> 0 )
{
printf("text 字符串ASCII 值大于 copy 字符串ASCII 值");
}
else if(strcmp(text,copy) < 0)
{
printf("text 字符串ASCII 值小于 copy 字符串ASCII 值");
}
else
{
printf("text 字符串ASCII 值等于 copy 字符串ASCII 值");
}
return 0;
}