字符串处理函数
1、字符串操作函数
以str开头的函数 都是字符串操作函数 都是遇到'\0'结束操作
1、测量字符串的长度 strlen
c
1 #include <string.h>
2 size_t strlen(const char *s);
3 s指 需要测量字符串的首元素地址
1 char str[128]="hello";
2 strlen(str) == 5;2、字符串拷贝函数strcpy
c
1 #include <string.h>
2 char *strcpy(char *dest, const char *src);
3 char *strncpy(char *dest, const char *src, size_t n);
4 dest:目的空间地址
5 src:原字符串的首元素地址
会导致拷贝越界
3、字符串追加函数strcat
c
1 #include <string.h>
2 char *strcat(char *dest, const char *src);
3 char *strncat(char *dest, const char *src, size_t n);将src指向的字符串 追加到 dest指向的字符串尾部
4、字符串比较strcmp
c
1 #include <string.h>
2 int strcmp(const char *s1, const char *s2);
3 int strncmp(const char *s1, const char *s2, size_t n);返回值:
>0 s1字符串 > s2字符串
<0 s1字符串 < s2字符串
==0 s1字符串==s2字符串
c
void test03()
2 {
3 char s1[128] = "";
4 char s2[128] = "";
5
6 printf("请输入第一个字符串:");
7 scanf("%s", s1);
8 printf("请输入第二个字符串:");
9 scanf("%s", s2);
10
11 if (strcmp(s1, s2) > 0)
12 {
13 printf("%s 大于 %s\n", s1, s2);
14 }
15 else if (strcmp(s1, s2) < 0)
16 {
17 printf("%s 小于 %s\n", s1, s2);
18 }
19 else if (strcmp(s1, s2) == 0)
20 {
21 printf("%s 等于 %s\n", s1, s2);
22 }
23 }5、字符查找函数strchr
c
1 #include <string.h>
2 char *strchr(const char *s, int c);
3 char *strrchr(const char *s, int c);
4 strchr从前往后找 第一次出现c的地址,如果没找到 返回NULL
6、字符串查找strstr
c
1 #include <string.h>
2 char *strstr(const char *haystack, const char *needle);
3 返回值:找到返回找到的地址 失败 返回NULL
7、字符串 转 数值
c
#include <stdlib.h>
atoi将字符串 转成 int类型
atol将字符串 转成 long类型
atof将字符串 转成 float类型
8、字符串 切割 strtok
c
1 #include <string.h>
2 char *strtok(char *str, const char *delim);第一次切割:str必须指向 待切割的字符串的首元素地址 delim指向分割符":"
后续切割:str传NULL delim指向分割符":"
返回值:
成功:返回值子串的首元素地址
失败:返回NULL
案例1:第一种切割法
c
1 void test06()
2 {
3 char str[] = "hehehe:xixixi:hahaha:lalala:heiheihei:henhenhen:wuwuwu";
4 char *buf[32]; //存放子串的首元素地址
5
6 //第一次切
7 int i = 0;
8 buf[i] = strtok(str, ":");
9
10 //后续切:上一次切割正常
11 while (buf[i] != NULL)
12 {
13 i++;
14 buf[i] = strtok(NULL, ":");
15 }
16
17 //遍历切割到的子串
18 i = 0;
19 while (buf[i] != NULL)
20 {
21 printf("%s\n", buf[i]);
22 i++;
23 }
24 }第二种

案例
c
1 char msg_src[]="+CMGR:REC UNREAD,+8613466630259,98/10/01,18:22:11+00,ABCdefGHI"
参考以下的函数名字以及参数,完成相应的要求
int msg_ deal(char *msg_ src, char *msg_ _done[],char *str)
参数1:待切割字符串的首地址
参数2:指针数组:存放切割完字符串的首地址
参数3:切割字符
返回值:切割的字符串总数量c
1 int msg_deal(char *msg_src, char *msg_done[], char *str)
2 {
3 int i = 0;
4 while ((msg_done[i] = strtok(msg_done[i], str)) && (++i))
5 ;
6
7 return i;
8 }
9 void test07()
10 {
11 char msg_src[] = "+CMGR:REC UNREAD,+8613466630259,98/10/01,18:22:11+00,ABCdefGHI";
12 char *msg_done[32] = {msg_src};
13 int num = 0;
14
15 num = msg_deal(msg_src, msg_done, ",");
16
17 printf("字符串的數量:%d\n", num);
18
19 int i = 0;
20 while (msg_done[i] != NULL)
21 {
22 printf("%s\n", msg_done[i++]);
23 }
24 }2、将字符串 转成 数值

3、格式化字符串
组包:按照需要的格式 组成字符串
解包:解析特定格式的数据
1、sprintf 用于组包
将零散的数据 按照固定的格式 组成字符串
c
1 #include <stdio.h>
2 int sprintf(char *str, const char *format,...);
3 sprintf返回值为实际组包的长度
将数字转化为字符串

2、sscanf 用于解包
%d提取数值 '0'~'9'

%s提取字符串 遇到'\0' 空格 回车

4、sscanf 高级用法
1、跳过数据%*d %*s
c
sscanf("1234 5678", "%*d %s", buf);//buf="56782、读指定宽度的数据:%[width]s %[width]d


3、%[a-z] 表示匹配 a 到 z 中任意字符(尽可能多的匹配)

4、%[aBc] 匹配 a、B、c 中一员,贪婪性

5、%【^aFc】匹配非 a Fc 的任意字符,贪婪性


综合 案例
c
1 #include <string.h>
2 void test08()
3 {
4 char buf[] = "+CMGR:REC UNREAD,+8613466630259,98/10/01,18:22:11+00,AB
CdefGHI";
5 char *msg[32] = {buf};
6
7 int i = 0;
8 while ((msg[i] = strtok(msg[i], ",")) && (++i))
9 ;
10
11 //短信的读取状态
12 //msg[0] = "+CMGR:REC UNREAD"
13 char status[128] = "";
14 sscanf(msg[0], "%*s %s", status); // +CMGR:REC UNREAD
15 if (strcmp(status, "UNREAD") == 0)
16 {
17 printf("有未读信息\n");
18 }
19 else if (strcmp(status, "READ") == 0)
20 {
21 printf("已读信息\n");
22 }
23
24 //msg[1]="+8613466630259"
25 printf("手机号:%s\n", msg[1] + 3);
26
27 //msg[2]="98/10/01"
28 int year = 0;
29 int month = 0;
30 int day = 0;
31 sscanf(msg[2], "%d/%d/%d", &year, &month, &day);
32 printf("日期:%d年%02d月%02d日\n", year + 1900, month, day);
33
34 //msg[3]="18:22:11+00"
35 int h = 0, m = 0, s = 0;
36 sscanf(msg[3], "%d:%d:%d", &h, &m, &s);
37 printf("时间:%02d时%02d分%02d秒\n", h, m, s); // %02d 不足左侧用0补齐
38
39 printf("收到的消息:%s\n", msg[4]);
40 }5、const
1、const修饰普通变量 为只读变量

2、const 修饰*
c
1 const int *p
2 在使用中:
3 *p是只读 不同通过*p 修改p所指向的空间内容
4 p 可读可写 p可以指向其他空间
3、const 修饰 指针变量
c
1 int * const p = #
2 p只读 除了初始化 不能修改p的指向
3 *p可读可写 可以通过*p修改p指向的空间内容
4、const 既修饰* 也修饰指针变量
c
const int *const p
p只读 *p也是只读
阿源的知识库