C语言-字符串处理函数

本文详细介绍了C语言中用于字符串操作的关键函数,如strlen计算字符串长度、strcpy和strncpy字符串复制、strcat追加、strcmp和strncmp比较、strchr和strstr查找、atoi和atof转换数值、strtok切割字符串、sprintf和sscanf格式化、以及const关键字的作用。
摘要由CSDN通过智能技术生成

、获取字符串长度函数

1 #include <string.h>

2 size_t strlen(const char *s);

3 功能:计算一个字符串的长度

4 参数:

5 s:指定的字符串

6 返回值:

7 当前字符串的长度

8 注意:strlen获取的字符串长度遇到第一个\0结束且\0不算做字符串长度之中

1 #include <stdio.h>

2 #include <string.h>

3

4 int main(int argc, char *argv[])

5 {

6 //使用strlen函数获取字符串的长度7 //strlen获取的字符串的长度遇到第一个\0结束

8 char s1[100] = "hel\0lo";

9

10 printf("s1_len = %d\n", strlen(s1));

11 printf("s1_size = %d\n", sizeof(s1));

12

13 char *s2 = "hello";

14

15 printf("s2_len = %d\n", strlen(s2));

16 printf("s2_size = %d\n", sizeof(s2));

17

18 return 0;

19 }

20

二、字符串拷贝函数

1 #include <string.h>

2 char *strcpy(char *dest, const char *src);

3 功能:将src复制给dest

4 参数:

5 dest:目的字符串

6 src:源字符串

7 返回值:

8 保存dest字符串的首地址

9 注意:使用strcpy函数复制字符串时必须保证dest足够大,否则会内存溢出

10 strcpy是将src字符串中第一个\0之前包括\0复制给dest

11

12 char *strncpy(char *dest, const char *src, size_t n);

13 函数的说明:

14 src指向的字符串前n个字节,拷贝到dest指向的内存中

15 返回值:

16 目的内存的首地址

17 注意:

18 1strncpy不拷贝 ‘\0

19 2、如果n大于src指向的字符串中的字符个数,则在dest后面填充nstrlen(src)’\0

1 #include <stdio.h>

2 #include <string.h>

3

4 int main(int argc, char *argv[])

5 {

6 //使用strcpy函数拷贝字符串

7 char s1[32] = "hello world";

8 //使用strcpy函数时,必须保证第一个参数的内存足够大

9 //char s1[5] = "abcd";

10 char s2[32] = "abcdefg";

11

12 strcpy(s1, s2);

13

14 printf("s1 = %s\n", s1);

15

16 int i;

17 for(i = 0; i < 32; i++)

18 {

19 printf("[%c] ‐ %d\n", s1[i], s1[i]);

20 }

21

22 return 0;

23 }

24

三、字符串追加函数

1 #include <string.h>

2 char *strcat(char *dest, const char *src);

3 功能:将src追加到dest的后面

4 参数:

5 dest:目的字符串

6 src:源字符串

7 返回值:

8 保存dest字符串的首地址

9

10 char *strncat(char *dest, const char *src, size_t n);

11 追加src指向的字符串的前n个字符,到dest指向的字符串的后面。

12 注意如果n 大于src的字符个数,则只将src字符串追加到dest指向的字符串的后面

13 追加的时候会追加’\0

1 #include <stdio.h>

2 #include <string.h>

3

4 int main(int argc, char *argv[])

5 {

6 //使用strcat函数追加字符串

7 char s1[32] = "hello world";

8 char s2[32] = "abcdef";

9

10 //strcat是从s1\0的位置开始追加,直到s2的第一个\0复制完毕后结束

11 strcat(s1, s2);

12

13 printf("s1 = %s\n", s1);

14

15 return 0;

16 }

四、字符串比较函数

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

4 功能:strcmp是比较两个字符串的内容,strncmp是比较两个字符串的前n个字节是否一样

5 参数:

6 s1s2:要比较的两个字符串

7 nstrncmp中的参数n表示要比较的字节数

8 返回值:

9 0 s1 = s2

10 >0 s1 > s2

11 <0 s1 < s2

1 #include <stdio.h>

2 #include <string.h>

3

4 int main(int argc, char *argv[])

5 {

6 //使用strcmp比较两个字符串的内容是否一致

7 //strcmp函数一个字符一个字符比较,只要出现不一样的,就会立即返回

8 char s1[] = "hello";

9 char s2[] = "w";

10

11 int ret = strcmp(s1, s2);

12

13 if(ret == 0)

14 {

15 printf("s1 = s2\n");

16 }

17 else if(ret > 0)

18 {

19 printf("s1 > s2\n");

20 }

21 else

22 {

23 printf("s1 < s2\n");

24 }

25

26 return 0;

27 }

五、字符查找函数

1 #include <string.h>

2 char *strchr(const char *s, int c);

3 功能:在字符指针s指向的字符串中,找ascii 码为c的字符

4 参数:

5 s:指定的字符串

6 c:要查找的字符

7 返回值:

8 成功:找到的字符的地址

9 失败:NULL

10 注意:s指向的字符串中有多个ASCIIc的字符,则找的是第1个字符

11

12 char *strrchr(const char *s, int c);

13 功能:在s指向的字符串中,找最后一次出现的ASCIIc的字符,

1 #include <stdio.h>

2 #include <string.h>

3

4 int main(int argc, char *argv[])

5 {

6 //使用strchr函数在一个字符串中查找字符

7 char s[] = "hel6lo wor6ld";

8 //找第一个匹配的字符

9 char *ret = strchr(s, '6');

10 //找最后一个匹配的字符

11 //char *ret = strrchr(s, '6');

12

13 if(ret == NULL)

14 {

15 printf("没有找到\n");

16 }

17 else18 {

19 printf("找到了,在数组的第%d个位置\n", ret s);

20 }

21

22 return 0;

23 }

六、字符串匹配函数

1 #include <string.h>

2 char *strstr(const char *haystack, const char *needle);

3 函数说明:

4 haystack指向的字符串中查找needle指向的字符串,也是首次匹配

5 返回值:

6 找到了:找到的字符串的首地址

7 没找到:返回NULL

1 #include <stdio.h>

2 #include <string.h>

3

4 int main(int argc, char *argv[])

5 {

6 //使用strstr函数在一个字符串中查找另一个字符串

7 char s[] = "1234:4567:666:789:666:7777";

8

9 //strstr查找的时候,查找的是第二个参数的第一个\0之前的内容

10 char *ret = strstr(s, "666");

11

12 if(ret == NULL)

13 {

14 printf("没找到\n");

15 }

16 else

17 {

18 printf("找到了,在当前字符串的第%d个位置\n", ret s);

19 }

20 return 0;

21 }

七、字符串转换数值

1 #include <stdlib.h>

2 int atoi(const char *nptr);

3 功能:将一个数字型字符串转化为整形数据

4 参数:

5 nptr:指定的字符串

6 返回值:

7 获取到的整形数据

1 #include <stdio.h>

2 #include <stdlib.h>

3

4 int main(int argc, char *argv[])

5 {

6 //使用atoi将数字型字符串转化为整形数据

7 char s1[] = "7856";

8 int ret1 = atoi(s1);

9

10 printf("ret1 = %d\n", ret1);

11

12 //使用atof将浮点型的字符串转化为浮点型数据

13 char s2[] = "3.1415926";

14 double ret2 = atof(s2);

15

16 printf("ret2 = %lf\n", ret2);

17

18 return 0;

19 }

八、字符串切割函数

1 #include <string.h>

2 char *strtok(char *str, const char *delim);

3 功能:对字符串进行切割

4 参数:

5 str:要切割的字符串

6 第一次切割,就传入指定的字符串,后面所有次的切割传NULL

7 delim:标识符,要根据指定的delim进行切割,切割的结果不包含delim

8 返回值:

9 返回切割下来的字符串的首地址,如果都切割完毕,则返回NULL

1 #include <stdio.h>

2 #include <string.h>

3

4 int main(int argc, char *argv[])

5 {

6 //使用strtok函数切割字符串

7 char s[] = "111:22222:33:4444444444:5555555555555";

8 char *ret;

9

10 //第一次切割

11 ret = strtok(s, ":");

12 printf("ret = %s\n", ret);

13

14 //后面所有切割时都要将strtok的第一个参数传NULL

15 while((ret = strtok(NULL, ":")) != NULL)

16 {

17 printf("ret = %s\n", ret);

18 }

19

20 return 0;

21 }

九、格式化字符串操作函数

1 #include <stdio.h>

2 int sprintf(char *str, const char *format, ...);

3 功能:将按照格式保存的字符串复制给str

4 参数:

5 str:保存字符串

6 format:同printf

7 返回值:

8 保存的字符串的字节数

9

10

11 #include <stdio.h>

12 int sscanf(const char *str, const char *format, ...);

13 功能:scanf是从终端读取数据并赋值给对应变量,而sscanf是从第一个参数中读取数据

14 参数:

15 str:指定要获取内容的字符串

16 format:按照格式获取数据保存在变量中

17 返回值:

18 成功获取的个数

9.1 sprintf和sscanf的基本用法

1 //sprintfsscanf的基本用法

2 void test1()

3 {

4 char buf[20];

5 int a, b, c;

6

7 sprintf(buf,"%d:%d:%d",2013,10,1);

8 printf("buf = %s\n",buf);

9

10 sscanf("2013:10:1", "%d:%d:%d", &a, &b, &c);

11 printf("a=%d,b=%d,c=%d\n",a,b,c);

12 }

9.2 sscanf高级用法

1 //sscanf高级用法

2 void test2()

3 {

4 //1、跳过数据:%*s%*d

5 char buf1[20];

6 sscanf("1234 5678","%*d %s",buf1);

7 printf("%s\n",buf1);

8

9 //2、读指定宽度的数据:%[width]s

10 char buf2[20];

11 sscanf("12345678","%4s ",buf2);

12 printf("%s\n",buf2);

13

14 //3、支持集合操作:只支持获取字符串

15 // %[a‐z] 表示匹配az中任意字符(尽可能多的匹配)

16 // %[aBc] 匹配aBc中一员,贪婪性

17 // %[^aFc] 匹配非aFc的任意字符,贪婪性

18 // %[^a‐z] 表示读取除a‐z以外的所有字符

19 char buf3[20];

20 sscanf("agcd32DajfDdFF","%[a‐z]",buf3);

21 printf("%s\n",buf3);

22 }

十、const

1 #include <stdio.h>

23 //const修饰全局变量

4 //此时全局变量只能使用但是不能修改,

5 //如果直接拿全局变量修改值,编译直接报错

6 //如果使用全局变量的地址修改值,运行时程序异常结束

7 const int a = 100;

8 void test1()

9 {

10 printf("a = %d\n", a);

11

12 //a = 666;

13 //printf("a = %d\n", a);

14

15 int *p = &a;

16 *p = 888;

17 printf("a = %d\n", a);

18 }

19

20 //const修饰普通局部变量

21 //可以读取变量的值

22 //不能直接通过变量进行修改值,编译报错

23 //可以通过变量的地址修改值

24 void test2()

25 {

26 const int b = 100;

27 printf("b = %d\n", b);

28

29 //b = 666;

30 //printf("b = %d\n", b);

31

32 int *p = &b;

33 *p = 888;

34 printf("b = %d\n", b);

35 }

36

37 //const修饰指针变量

38 //如果const修饰指针变量的类型,无法通过指针变量修改地址里面的值

39 //如果const修饰指针变量,无法修改指针变量保存的地址

40 //如果const既修饰指针变量的类型,又修饰指针变量,则只能通过原本变量修改值

41 void test3()

42 {

43 int c = 100;

44 //const修饰指针变量的类型

45 //const int * p = &c;

46 //const修饰指针变量

47 //int * const p = &c;

48 //const既修饰指针变量的类型,又修饰指针变量

49 const int * const p = &c;

50 printf("*p = %d\n", *p);

51

52 c = 666;

53 printf("*p = %d\n", *p);

54

55 *p = 777;

56 printf("*p = %d\n", *p);

57

58 int d = 888;

59 p = &d;

60 printf("*p = %d\n", *p);

61 }

62

63 int main(int argc, char *argv[])

64 {

65 test3();

66

67 return 0;

68 }

69

  • 24
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

做一道光

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值