7. 字符串
7.1 获取字符串的长度函数 - strlen
| 头文件: #include <string.h> |
| 函数定义: size_t strlen(const char *s) |
| 参数:s - 指定的字符串 |
| 返回值:当前字符串的长度 |
| #include <stdio.h> |
| #include <string.h> |
| int main(int argc, char const *argv[]) |
| { |
| |
| |
| char s[100] = "hello world"; |
| printf("s1_len = %d\n",strlen(s)); |
| return 0; |
| } |
输出结果
7.2 字符串拷贝函数 - strcpy
| 头文件: #include <string.h> |
| 函数的定义: char *strcpy(char *dest, const char *sre); |
| 函数的说明: |
| 拷贝 sc 指向的字符串到 dest指针指向的内存中,"\0"也会拷贝 |
| 函数的返回值: |
| 目的内存的地址 |
| 注意: 在使用此函数的时候,必须保证 dest 指向的内存空间足够大,否则会出现内存污染。 |
| |
| char *strncpy(char *dest,const char *src,size_t n); |
| 函数的说明:将src指向的字符串前n个字节,拷贝到dest指向的内存中 |
| 返回值:目的内存的首地址 |
| 注意: |
| 1、strncpy不拷贝 "\0" |
| 2、如果n大于src指向的字符串中的字符个数,则在dest后面填充 n-strlen(src) 个 "\0" |
| #include <stdio.h> |
| #include <string.h> |
| int main(int argc, char const *argv[]) |
| { |
| char s1[100] = "hello world"; |
| char s2[30] = "dasfasfags"; |
| |
| |
| strcpy(s1, s2); |
| printf("s1 = %s\n", s1); |
| return 0; |
| } |
输出结果
7.3 字符串追加函数 - strcat
| 头文件:#include <string.h> |
| 函数定义:char *strcat(char *deet, const char *src); |
| 函数功能: strcat,函数追加 src字符串到 dest,指向的字符串的后面。追加的时候会追加"\0" |
| 注意:保证 dest 指向的内存空间足够大。 |
| |
| char *strncat(char *dest, const char *src, size_t n); |
| 追加 src指向的字符串的前 n个字符到 dest指向的字符串的后面。 |
| 注意: 如果 n 大于 src,的字符个数,则只将 src字符串追加到dest, 指向的字符串的后面追加的时候会追加"\0" |
| #include <stdio.h> |
| #include <string.h> |
| int main(int argc, char *argy[]) |
| { |
| |
| char s1[32] = "hello world"; |
| char s2[32] = "abcdef"; |
| |
| strcat(s1, s2); |
| printf("sl =%s\n", s1); |
| return 0; |
| } |
输出结果
7.4 字符串比较函数 - strcmp
| |
| 头文件:#include<string.h> |
| 函数定义:int strcmp(const char *sl,const char *s2); |
| 函数说明: 比较 s1 和 s2 指向的字符串的大小, |
| 比较的方法: 逐个字符去比较 ascII 码,一旦比较出大小返回 |
| 返回值: |
| 1. 如果 s1 指向的字符串大于 s2 指向的字符串 返回 1; |
| 2. 如果 s1 指向的字符串小于 s2 指向的字符串 返回-1; |
| 3. 如果相等的话返回 0; |
| |
| strncmp( const char *sl,const char *s2,size_t n) - 比较前n个字符 |
| #include <stdio.h> |
| #include <string.h> |
| int main(int argc, char *argy[]) |
| { |
| |
| char s1[32] = "hella"; |
| char s2[32] = "hello"; |
| |
| int ret = strcmp(s1, s2); |
| |
| if (ret == 0){ |
| printf("s1 = s2 \n"); |
| }else if (ret>0) |
| { |
| printf("s1 > s2 \n"); |
| }else{ |
| printf("s1 < s2 \n"); |
| } |
| |
| return 0; |
| } |
输出结果
7.5 字符串查找函数 - strchr
| 头文件:#include <string.h> |
| char *strchr(const char *s,int c); |
| 功能:在字符指针s指向的字符串中,找ascii码为c的字符 |
| 参数: |
| s:指定的字符串 |
| c:要查找的字符返回值: |
| 成功:找到的字符的地址 |
| 失败:NULL注意:s指向的字符串中有多个ASCII为c的字符,则找的是第1个字符 |
| |
| char *strrchr(const char *s,int c); |
| 功能:在s指向的字符串中,找最后一次出现的ASCII为c的字符, |
| #include <stdio.h> |
| #include <string.h> |
| int main(int argc, char *argy[]) |
| { |
| |
| char s1[32] = "hella6dasdas"; |
| char *ret = strchr(s1,'6'); |
| if(ret == NULL){ |
| printf("NOT FOUND\n"); |
| } else { |
| printf("GOT , IN POSITINO %d ! \n", ret -s1 ); |
| } |
| |
| return 0; |
| } |
输出结果
7.6 字符串匹配函数 - strstr
| 头文件:#include <string.h> |
| char *strstr(const char *haystack, const char *needle); |
| 函数说明: 在 haystack 指向的字符串中查找 needle 指向的字符串,也是首次匹配 |
| 返回值: |
| 1. 找到了: 找到的字符串的首地址 |
| 2. 没找到: 返回 NULL |
| #include <stdio.h> |
| #include <string.h> |
| int main(int argc, char *argy[]) |
| { |
| |
| char s1[32] = "hella6dasdas"; |
| char *ret = strstr(s1, "6d"); |
| if (ret == NULL) |
| { |
| printf("NOT FOUND\n"); |
| } |
| else |
| { |
| printf("GOT , IN POSITINO %d ! \n", ret - s1); |
| } |
| |
| return 0; |
| } |
输出结果
7.7 字符串转换为数值 - atoi
| atoi/atol/atof |
| 头文件:#include <stdlib.h> |
| 函数的定义:int atoi(const char *nptr); |
| 函数的功能: 将 nptr 指向的字符串转换成整数,返回 |
| #include <stdio.h> |
| #include <stdlib.h> |
| |
| int main(int argc, char *argy[]) |
| { |
| |
| char s1[32] = "12314"; |
| int ret1 = atoi(s1); |
| |
| printf("ret1 = %d \n", ret1); |
| |
| char s2[] = "3.1456414"; |
| double ret2 = atof(s2); |
| printf("ret2 = %lf \n", ret2); |
| |
| return 0; |
| } |
输出结果
| ret1 = 12314 |
| ret2 = 3.145641 |
7.8 字符串切割函数 - strtok
| #include<string.h> |
| char *strtok(char *str,const char *delim); |
| 功能:对字符串进行切割 |
| 参数: |
| str:要切割的字符串第一次切割,就传入指定的字符串,后面所有次的切割传NULL |
| delim:标识符,要根据指定的delim进行切割,切割的结果不包含delim返回值: |
| 返回切割下来的字符串的首地址,如果都切割完毕,则返回NULL |
| #include <stdio.h> |
| #include <string.h> |
| |
| int main(int argc, char *argy[]) |
| { |
| |
| char s1[32] = "12314:dasda1:54131das:31321das"; |
| char *ret1; |
| ret1 = strtok(s1, ":"); |
| |
| printf("ret1 = %s \n", ret1); |
| |
| while ((ret1 = strtok(NULL, ":")) != NULL) |
| { |
| |
| printf("ret1 = %s \n", ret1); |
| } |
| |
| return 0; |
| } |
| ret1 = 12314 |
| ret1 = dasda1 |
| ret1 = 54131das |
| ret1 = 31321das |
7.9 格式化字符串操作函数
| #include <stdio.h> |
| int sprintf(char *str,const char *format,...); |
| 功能:将按照格式保存的字符串复制给str |
| 参数: |
| str:保存字符串 |
| format: 同printf |
| 返回值: |
| 保存的字符串的字节数 |
| |
| #include<stdio.h> |
| int sscanf(const char *str,const char *format,...) |
| 功能:scanf是从终端读取数据并赋值给对应变量,而sscanf是从第一个参数中读取数据 |
| 参数: |
| str:指定要获取内容的字符串 |
| format:按照格式获取数据保存在变量中 |
| 返回值: |
| 成功获取的个数 |
| #include <stdio.h> |
| |
| void test1(){ |
| char buf[20]; |
| int a,b,c; |
| |
| sprintf(buf,"%d:%d:%d",2013,10,1); |
| printf("buf = %s\n",buf); |
| } |
| |
| void test2(){ |
| |
| char buf1[20]; |
| sscanf("1234 5678","%*d %s",buf1); |
| printf("%s\n",buf1); |
| |
| |
| char buf2[20]; |
| sscanf("12345678","%4s",buf2); |
| printf("%s\n",buf2); |
| |
| |
| |
| |
| |
| |
| |
| char buf3[20]; |
| sscanf("agcd32DajfDdFF","%[a-z]",buf3); |
| printf("%s\n",buf3); |
| return 0; |
| } |
| |
| int main(int argc, char const *argv[]) |
| { |
| test1(); |
| test2(); |
| return 0; |
| } |
| |
输出结果
| buf = 2013:10:1 |
| 5678 |
| 1234 |
| agcd |
7.10 const
| |
| const int a=100; |
| a=200; |
| |
| |
| |
| const char *str |
修饰全局变量
| |
| |
| |
| |
| const int a =100; |
| void test1(){ |
| printf("a = %d\n",a); |
| a = 6600; |
| |
| int *p = &a; |
| *p = 888; |
| printf("a = %d\n",a); |
| } |
修饰局部变量
| |
| |
| |
| void test2(){ |
| const int b = 100; |
| b = 666; |
| printf("b = %d\n",b); |
| |
| int *p = &a; |
| *p = 888; |
| printf("a = %d\n",a); |
| } |
修饰指针变量
| |
| const int *p = &c; |
| |
| int * const p = &c; |
| |
| const int * const p = &c; |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步