考试日复习【字符串处理函数】
只要是以str开头的函数 都是遇到’\0’结束
#include<string.h>
1、strlen测量字符换长度
size_t strlen(const char *s)
//s:被测量的字符串首元素地址
//返回值为 字符串的长度 不包含’\0’
//const char *s strlen函数 不会通过s修改s指向的空间内容
案例:
void test01()
{
char buf1[128]="hehehe";
char buf2[]="hehehe";
char buf3[]="hehe\0he";
// \123 代表一个字符 \hhh 八进制转义h: 0~7
//\\表示'\'
char buf4[]="hehe\123\\he";
//\\x2f表示一个字符 \xdd 十六进制转义 d:0~9 a~f
char buf5[]="hehe\x2fhe";
printf("%d\n",sizeof(buf1));//126
printf("%d\n",strlen(buf1));//6
printf("%d\n",sizeof(buf2));//7
printf("%d\n",strlen(buf2));//6
printf("%d\n",sizeof(buf3));//8
printf("%d\n",strlen(buf3));//4
printf("%d\n",sizeof(buf4));//9
printf("%d\n",strlen(buf4));//8
printf("%s\n",buf4);
printf("%d\n",sizeof(buf5));//8
printf("%d\n",strlen(buf5));//7
char buf6[]="\0hehe\0hehe";
printf("%d\n",strlen(buf6));//0
return;
}
作业:自定义一个my_strlen函数测量字符串长度(不能在函数里面使用strlen)
int my_strlen(const char *s);
2、strcpy strncpy字符串拷贝(重要)
strcpy
原型:char *strcpy( char *dest, const char *src )
功能:把src所指向的字符串复制到dest所指向的空间中
返回值:返回dest字符串的首地址
注意:遇到’\0’会结束,只是’\0’也会拷贝过去
void test02()
{
char src[]="hello string";
//保证dst足够大
char dst[128]="";
strcpy(dst,src);
printf("dst=%s\n",dst);//"hello string"
}
void test02()
{
char src[]="hello\0string";
//保证dst足够大
char dst[128]="";
strcpy(dst,src);
printf("dst=%s\n",dst);//"hello"
}
实现strcpy
char *my_strcpy(char *dst,const char *src)
{
#if 0
do
{
*dst = *src;
dst++;
src++;
}while(*src != '\0');
*dst='\0';
#endif
while(*dst++ = *src++);
}
void test02()
{
char src[]="hello\0string";
//保证dst足够大
char dst[128]="";
my_strcpy(dst,src);
printf("dst=%s\n",dst);//"hello"
}
strncpy
原型:char *strncpy( char *dest, const char *src,int num)
功能:把src指向字符串的前num个复制到dest所指向的空间中
返回值:返回dest字符串的首地址
注意:’\0’不拷贝
void test03()
{
char src[]="hello string";
//保证dst足够大
char dst[128]="";
strncpy(dst,src,3);
printf("dst=%s\n",dst);//"hel"
}
void test03()
{
char src[]="hello";
//保证dst足够大
char dst[128]="";
strncpy(dst,src,30);
printf("dst=%s\n",dst);//"hello"
}
3、strcat字符串的拼接
char *strcat(char *dest, const char *src);
将src的字符串 拼接到 dst的末尾(dst第一个’\0’的位置)
void test04()
{
char src[]="world";
char dst[128]="hello";
strcat(dst,src);
printf("%s\n",dst);//"helloworld"
}
void test04()
{
char src[]="wor\0ld";
char dst[128]="hello";
strcat(dst,src);
printf("%s\n",dst);//"hellowor"
}
strncat拼接前n个
void test04()
{
char src[]="world";
char dst[128]="hello";
strncat(dst,src,2);
printf("%s\n",dst);//"hellowo"
}
作业:自定义my_strcat函数 不许使用 strcat
char *my_strcat(char *dest, const char *src);
4、strcmp字符串的比较 (整个字符串的比较)
int strcmp(const char *s1, const char *s2);
功能:将s1和s2指向的字符串 逐个字符比较
返回值:
>0: 表示s1 > s2
<0: 表示s1 < s2
==0: 表示s1 == s2
void test05()
{
char s1[]="hehe haha";
char s2[]="hehe xixi";
char s3[]="hehe haha";
if(strcmp(s1,s2) >0 )
{
printf("s1 > s2\n");
}
else if(strcmp(s1,s2) <0 )
{
printf("s1 < s2\n");
}
else if(strcmp(s1,s2) == 0)
{
printf("s1 == s2\n");
}
if(strcmp(s1,s3) >0 )
{
printf("s1 > s3\n");
}
else if(strcmp(s1,s3) <0 )
{
printf("s1 < s3\n");
}
else if(strcmp(s1,s3) == 0)
{
printf("s1 == s3\n");
}
}
运行结果:
strncmp 字符串局部比较
void test05()
{
char s1[]="hehe haha";
char s2[]="hehe xixi";
if(strncmp(s1,s2,4) >0 )
{
printf("s1 > s2\n");
}
else if(strncmp(s1,s2,4) <0 )
{
printf("s1 < s2\n");
}
else if(strncmp(s1,s2,4) == 0)
{
printf("s1 == s2\n");
}
}
运行结果:
5、strchr字符查找函数
char *strchr(const char *s, int c);
//从字符串s中查找第一次c字符出现的地址
//没有找到 返回NULL
void test06()
{
char str[]="www.1000phone.com";
char *ret = NULL;
ret = strchr(str,'o');
if(ret != NULL)
{
*ret = '#';
}
printf("str=%s\n",str);//"www.1000ph#ne.com";
}
(了解)
void test06()
{
char str[]="www.1000phone.com";
char *ret = NULL;
while((ret = strchr(str,'o')) && (*ret = '#') );
printf("str=%s\n",str);//"www.1000ph#ne.c#m";
}
重要 啥都写 了解
6、strstr字符串查找
char *strstr(const char *s1, const char *s2);
//从s1中查找字符串s2 返回第一次s2出现的地址
//查找失败 返回NULL
void test07()
{
char s1[]="www.sex.777.sex.com";
char s2[]="sex";
char *ret = NULL;
ret = strstr(s1,s2);
if(ret == NULL)
{
return;
}
printf("%s\n",ret);//"sex.777.sex.com"
}
void test07()
{
char s1[]="www.sex.777.sex.com";
char s2[]="sex";
char *ret = NULL;
while(1)
{
ret = strstr(s1,s2);
if(ret == NULL)
{
break;
}
memset(ret,'#', 3);
}
printf("%s\n",s1);//"www.###.777.###.com"
}