苏州建厂,潮州seo建站,国家商标注册查询网官网,石岩做网站哪家好1.测字符串长度函数 头文件#xff1a; #include string.h 函数定义#xff1a; size_t strlen(const char *s); 函数功能#xff1a; 测字符指针 s 指向的字符串中字符的个数#xff0c;不包括 ’\0’ void fun01()
{char *num hello;int len …1.测字符串长度函数 头文件 #include string.h 函数定义 size_t strlen(const char *s); 函数功能 测字符指针 s 指向的字符串中字符的个数不包括 ’\0’ void fun01()
{char *num hello;int len strlen(num);printf(%d\n,len);
} 2.字符串拷贝函数 头文件 #include string.h 函数的定义 char *strcpy(char *dest, const char *src); 函数的说明 拷贝 src 指向的字符串到 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’ void fun02()
{char *num1 helloworld;char *num2 (char *)malloc(strlen(num11));if(num2 NULL){printf(内存开辟失败);return;}strcpy(num2,num1);printf(%s\n,num2);if(num2 ! NULL){free(num2);num2 NULL;}} 3、字符串追加函数 头文件 #include string.h 函数定义 char *strcat(char *dest, const char *src); 函数功能 strcat 函数追加 src 字符串到 dest 指向的字符串的后面。追加的时候会追加 ’\0’ 注意保证 dest 指向的内存空间足够大。 void fun07()
{char str01[15] hello;char *str02 world;strcat(str01,str02);printf(%s\n,str01);
} 4.字符串比较函数 strcmp/strncmp // 比较 头文件 #include string.h 函数定义 :int strcmp(const char *s1, const char *s2); 函数说明 比较 s1 和 s2 指向的字符串的大小 比较的方法逐个字符去比较 ascII 码一旦比较出大小返回。 如过所有字符都一样则返回 0 返回值 如果 s1 指向的字符串大于 s2 指向的字符串 返回 1 如果 s1 指向的字符串小于 s2 指向的字符串 返回 -1 如果相等的话返回 0 int strncmp(const char *s1, const char *s2, size_t n); 函数说明比较 s1 和 s2 指向的字符串中的前 n 个字符 void fun03()
{char str01[] hello;char *str02 (char *)malloc(strlen(str01)1);strcpy(str02,str01);str01[0] H;strcmp(str01,str02);printf(str02 %s\n,str02);
} void fun09()
{char *str01 helloworld;char *str02 hello c;int x strncmp(str01,str02,6);printf(x %d\n,w);
} 5.字符查找函数 头文件 #include string.h 函数定义 char *strchr(const char *s, int c); 函数说明 在字符指针 s 指向的字符串中找 ascii 码为 c 的字符 注意是首次匹配如果过说 s 指向的字符串中有多个 ASCII 为 c 的字符则找的是第 1 个字符 返回值 找到了返回找到的字符的地址 找不到返回 NULL 函数定义 char *strrchr(const char *s, int c); 函数的说明末次匹配 在 s 指向的字符串中找最后一次出现的 ASCII 为 c 的字符 返回值 末次匹配的字符的地址。 void fun12()
{char *str abcdellll;char *p1 strstr(str,cde);printf(%ld\n,p1-str);
} 6.字符串匹配函数 #include string.h char *strstr(const char *haystack, const char *needle); 函数说明 在 haystack 指向的字符串中查找 needle 指向的字符串也是首次匹配 返回值 找到了找到的字符串的首地址 每找到返回 NULL void fun10()
{char *str01 hello ;char *pl strchr(str01,l);printf(%p\n,pl);if(pl ! NULL){printf(%c\n,*pl);printf(%d\n,pl-str01);}
} 7.字符串转换数值 atoi/atol/atof // 字符串转换功能 头文件 #include stdlib.h 函数的定义 int atoi(const char *nptr); 函数的功能 将 nptr 指向的字符串转换成整数返回 例 8 int num; numatoi(“123”); 则 num 的值为 123 long atol(const char *nptr); double atof(const char *nptr); void fun13()
{int nums atoi(123);printf(%d\n,nums);
} 8.字符串切割函数 头文件 #include string.h 函数定义 char *strtok(char *str, const char *delim); 函数的功能 字符串切割按照 delim 指向的字符串中的字符切割 str 指向的字符串。 其实就是在 str 指向的字符串中发现了 delim 字符串中的字符就将其变成 ’\0’, 调用一次 strtok 只切割一次切割一次之后再去切割的时候 strtok 的第一个参数 传 NULL 意思是接着上次切割的位置继续切 注意如果 str 字符串中出现了连续的几个 delim 中的字符则只将第一个字符变成 ’\0’ void fun20()
{char s[] 123,,,...456##..789,,..;char *buf[3] {s,NULL};int i 0;while(1){buf[i] strtok(buf[i],,.#);if (buf[i] NULL){break;}i;}for(int j 0; j 3; j){printf(%s\n,buf[j]);}
} 9.格式化字符串操作函数 int sprintf(char *buf, const char *format, … ); \\ 输出到 buf 指定的内存区域。 例 char buf[20]; sprintf(buf,%d:%d:%d,2013,10,1); printf(“buf%s\n”,buf); void fun23()
{ //int sprintf (char *__restrict __s,const char *__restrict __format, ...)//参数://s:写入的内存的首地址//format:格式化语句//值char str[100];sprintf(str,%d年%d月%d日,2023,11,6);printf(%s\n,str);
} int sscanf(const char *buf,const char *format, … ); \\ 从 buf 指定的内存区域中读入信息 例 int a, b, c; sscanf(2013:10:1, %d:%d:%d, a, b, c); printf(“%d %d %d\n”,a,b,c); void fun27()
{char *str 1234567890;int num 0;char c 0;char s[3];sscanf(str,%3d%c%s,num,c,s);printf(%d\n,num);printf(%c\n,c);printf(%c\n,*s);
}sscanf 高级用法 1 、跳过数据 %*s 或 %*d 例 sscanf(1234 5678, %*d %s, buf); 2 、读指定宽度的数据 %[width]s void fun28()
{char *str 1234567890;int num 0;sscanf(str,%*2d%2d,num);printf(%d\n,num);
} 3 、支持集合操作只支持获取字符串 %[a-z] 表示匹配 a 到 z 中任意字符 ( 尽可能多的匹配 ) %[aBc] 匹配 a 、 B 、 c 中一员贪婪性 %[^aFc] 匹配非 a Fc 的任意字符贪婪性 %[^a-z]表示读取除 a-z 以外的所有字符 贪婪性 void fun29()
{char *str abcdedfA123abc;char p[100];sscanf(str,%[a-z],p);printf(%s\n,p);
} void fun30()
{char *str abcdedfA123abc;char p[100];sscanf(str,%*[a-z]%[^a-z],p);printf(%s\n,p);
} void fun31()
{char *str aaaBccBaedf;char p[100];sscanf(str,%[aBc],p);printf(%s\n,p);} 10.const 1: 修饰普通变量代表只读的意思 const int a100;// 定义了一个只读变量 a 值为 100 以后在程序中不能再给 a 赋值了 a200;// 错误的 a 只读 2 const 修饰指针 (1) 、 const char *str 意思是 str 指向的内存的内容不能通过 str 来修改 用来保护 str 指向的内存的内容 但是 str 的指向是可以改变的 char * strcpy(char *dest,const char *src); (2) 、 char * const str 意思是 str 是只读的变量 str 不能指向别的地方 但是 str 指向的内存的内容是有可能可以修改的 (3) 、 const char * const str str 不能指向别的地方指向的内存的内容也不能通过 str 去修改