目录
知识点8【atoi atol atof 】将字符串转化为数值
C语言string库函数中,大多以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;
}
知识点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"
}
知识点4【strncat拼接前n个】
void test04()
{
char src[]="world";
char dst[128]="hello";
strncat(dst,src,2);
printf("%s\n",dst);//"hellowo"
}
知识点5【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");
}
}
运行结果:
知识点6【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";
}
知识点7【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"
}
知识点8【atoi atol atof 】将字符串转化为数值
头文件:stdlib.h
#include<stdio.h>
#include<stdlib.h>//atoi atol
void test01()
{
printf("%d\n",atoi("123"));
printf("%ld\n",atol("12345"));
printf("%f\n",atof("12.3"));
return;
}
int main(int argc,char *argv[])
{
test01();
return 0;
}
运行结果:
自定义函数实现atoi的功能:
int my_atoi(char *buf)
{
//buf="1234"
int sum = 0;
while(*buf != '\0' && (*buf>='0' && *buf<='9'))
{
sum = sum*10+(*buf++)-'0';
}
return sum;
}
void test02()
{
char buf[128]="";
printf("请输入字符串:");
scanf("%s",buf);
printf("%d\n",my_atoi(buf));
return;
}
int main(int argc,char *argv[])
{
test02();
return 0;
}
运行结果:
知识点9【字符串切割函数 strtok】
char *strtok(char *str, const char *delim);
第一次切割: str必须指向要切割的字符串首元素地址 delim指向要切割的符号
第2~n次切割:str必须指向NULL delim指向要切割的符号
返回值:切割成功 返回切割到字符换片段的首元素地址 失败:NULL
注意:strtok不能切割字符串常量
"hehe:haha:xixi:lala"
如果切割完成会产生 "hehe" "haha" "xixi" "lala"
一般选择 char *arr[] 指针数组 来存放上面 独立的字符串的首元素地址
案例1:
void test03()
{
char buf[]="hehehe:haha:xixi:lala:heihei:henhen";
char *arr[32]={NULL};
int i=0;
//第1次切割
arr[i] = strtok(buf, ":");
//第2~n切割
while(arr[i] != NULL)//保证上一次切割正常 才有进行下一次切割的必要
{
i++;
arr[i] = strtok(NULL,":");
}
//遍历切割的内容
i=0;
while(arr[i] != NULL)
{
printf("%s\n",arr[i]);
i++;
}
}
运行结果:
案例:
void test04()
{
char buf[]="hehehe:haha:xixi:lala:heihei:henhen";
char *arr[32]={buf,NULL};
int i=0;
//切割
while(1)
{
arr[i] = strtok(arr[i],":");
if(arr[i] == NULL)
break;
i++;
}
//遍历切割的内容
i=0;
while(arr[i] != NULL)
{
printf("%s\n",arr[i]);
i++;
}
}
案例:
void test05()
{
char buf[]="hehehe:haha:xixi:lala:heihei:henhen";
char *arr[32]={buf,NULL};
int i=0;
//切割
while(arr[i++] = strtok(arr[i],":"));
//遍历切割的内容
i=0;
while(arr[i] != NULL)
{
printf("%s\n",arr[i]);
i++;
}
}
注意:
void test05()
{
char buf[]="hehehe:haha#xixi@lala:::::::heihei####henhen";
char *arr[32]={buf,NULL};
int i=0;
//切割
while(arr[i++] = strtok(arr[i],":#@"));
//遍历切割的内容
i=0;
while(arr[i] != NULL)
{
printf("%s\n",arr[i]);
i++;
}
}