首页 > 学院 > 开发设计 > 正文

3.2 字符串函数的实现

2019-11-08 20:21:04
字体:
来源:转载
供稿:网友

1. strlen

需要引入 string.h 头文件

#include <string.h>

1.1 使用strlen

size_t strlen(const char *s); 返回 s 的字符串长度(不包括结尾的 /0)

#include<stdio.h>#include<string.h>int main(int argc,char const *argv[]){ char line[] = "Hello"; PRintf("strlen=%d/n", strlen(line)); // strlen=5 printf("sizeof=%d/n", sizeof(line)); // sizeof=6 return 0;}

1.2 自定义mylen

#include<stdio.h>#include<string.h>size_t mylen(const char* s){ int index = 0; while(s[index] != '/0'){ index++; } return index;}int main(int argc,char const *argv[]){ char line[] = "Hello"; printf("strlen=%d/n", strlen(line)); // strlen=5 printf("sizeof=%d/n", sizeof(line)); // sizeof=6 printf("mylen=%d/n", mylen("Hello")); // mylen=5 return 0;}

2. strcmp

int strcmp(const char *s1, const char *s2);比较两个字符串,返回 0:s1 == s21:s1 > s2-1:s1 < s2

2.1 使用strcmp

#include<stdio.h>#include<string.h>int main(int argc,char const *argv[]){ char s1[] = "abc"; // s1 存的是 abc/0 char s2[] = "abc"; printf("%d/n", s1 == s2); // 0 s1==s2 比较的是地址 printf("%d/n", strcmp(s1, s2)); // 0 strcmp 比较的是ASCII码值,空格的 ASCII是32,/0的ASCII码值是 0 char s3[] = "bbc"; printf("%d/n", strcmp(s1, s3)); // -1 char s4[] = "Abc"; printf("%d", strcmp(s1, s4)); // 1 return 0;}

2.2 自定义mycmp

#include<stdio.h>#include<string.h>int mycmp1(const char *s1, const char *s2){ int index = 0; while(1){ if(s1[index] != s2[index]) { break; } else if( s1[index] == '/0') { break; } index++; } return s1[index] - s2[index]; } int mycmp2(const char *s1, const char *s2){ int index = 0; while(s1[index] == s2[index] && s1[index] != '/0'){ index++; } return s1[index] - s2[index];}int mycmp(const char* s1, const char* s2){ while(*s1 == * s2 && *s1 != '/0'){ s1++; s2++; } return *s1 - *s2;}int main(int argc,char const *argv[]){ char s1[] = "abc"; char s2[] = "ab"; printf("%d/n", mycmp(s1, s2)); // 99 char s3[] = "Abc"; printf("%d/n", mycmp(s1, s3)); // 32 printf("%d/n", 'a' - 'A'); // 32 return 0;}

3. strcpy

char* strcpy(char *restrict des, const char *restrict src);把 src 拷贝到 desrestrict 表明 des 和 src 不重叠(C99),多核复制时 重叠会出错返回des

3.1 使用 strcpy

#include<stdio.h>#include<stdlib.h>#include<string.h>int main(int argc,char const *argv[]){ char src[] = "Hello"; char *dst = (char*)malloc(strlen(src) + 1); strcpy(dst, src); printf("%c/n", dst[0]); return 0;}

3.2 自定义 mycpy

#include<stdio.h>#include<string.h>char* mycpy1(char *dst, const char *src){ int index = 0; while(src[index]){ dst[index] = src[index]; index++; } dst[index] = '/0'; return dst;}char* mycpy2(char* dst, const char* src){ while(*src != '/0'){ *dst = *src; dst++; src++; } *dst = '/0'; return dst;}char* mycpy3(char* dst, const char* src){ char* ret = dst; while(*src != '/0'){ *dst++ = *src++; } *dst = '/0'; return ret;}char* mycpy(char* dst, const char* src){ char* ret = dst; while(*dst++ = *src++){ ; } *dst = '/0'; return ret;}int main(int argc,char const *argv[]){ char s1[] = "Hello"; char s2[10]; mycpy(s2, s1); printf("%c/n", s2[1]); // e return 0;}

4. strchr 与 strrchr

char* strchr(const char *s, int c); // 查找某字符在字符串中首次出现的位置char * strrchr(const char *str, int c); // 函数用于查找某字符在字符串中最后一次出现的位置返回NULL表示没用找到

4.1 使用strchr 与 strrchr

#include<stdio.h>#include<string.h>int main(int argc,char const *argv[]){ char s[] = "Hello"; char *p = strchr(s, 'l'); printf("%s/n", p); // llo printf("%s/n",strrchr(s, 'l')); // lo 查找最后一个 l // 查找第2个 l char s1[] = "Helllo"; char *q = strchr(s1, 'l'); q = strchr(q + 1, 'l'); printf("%s/n", q); // llo return 0;}

5.strstr 与 strcasestr

char* strstr(const char *s1, const char *s2); // 查找s2 在s1 中的第一次出现的位置,返回s1的其余部分(从匹配点)。如果未找到所搜索的字符串,则返回 false。s2为空,返回s1char* strcasestr(const char *s1, const char *s2); // 忽略大小写,不是标准库函数#include<stdio.h>#include<string.h>int main(int argc,char const *argv[]){ char s1[] = "HelloWorld---"; char s2[] = "World"; char* ret1 = strstr(s1, s2); printf("%s/n", ret1); // World--- return 0;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表