|
函数名: strchr
原 型: char *strchr(char *str, char ch);
程序示例:printf("%s\n",strchr("hello world",'l')); //输出llo world
- char* my_strchr(char* str, char ch)
- {
- int i = 0;
- while (str[i])
- {
- if (str[i] != '\0')
- {
- return &str[i];
- }
- i++;
- }
- return NULL;
- }
- int main()
- {
- char str[] = "hello world";
- char* p = my_strchr(str, 'l');
- if (p == NULL)
- {
- printf("未找到 \n");
- }
- else
- {
- printf("%s \n",p);
- }
- return EXIT_SUCCESS;
- }
复制代码
第二种写法
- char* my_strchr(char* str, char ch)
- {
- while (*str)
- {
- if (*str == ch)
- {
- return str;
- }
- str++;
- }
- return NULL;
- }
复制代码
函数名: strstr
原 型: char *strstr(char *str1, char *str2);
程序示例:
printf("%s \n", strstr("abcdef123", "c")); //结果显示为:cdef123
printf("%s \n", strstr("abcdef123", "12")); //结果为123
- char* my_strstr(char* str1, char* str2)
- {
- char* fstr = str1;
- char* rstr = str1;
- char* tstr = str2;
- while (*fstr)
- {
- rstr = fstr;
- while (*fstr==*tstr&&*fstr!='\0')
- {
- fstr++;
- tstr++;
- }
- if (*tstr == '\0')
- {
- return rstr;
- }
- tstr = str2;
- fstr = rstr;
- fstr++;
- }
- return NULL;
- }
- int main()
- {
- char str1[] = "hello world";
- char str2[] = "llo";
- char* p=my_strstr(str1, str2);
- printf("%s \n", p);
- return EXIT_SUCCESS;
- }
复制代码
函数名: strspn 查找非str2字符集的字符出现的第一个位置的下标
原 型: int strspn(char *str1, char *str2);
printf("%d \n", strspn("111cde123", "12")); //结果为3
printf("%d \n", strspn("cdccc1def123", "cd"));//结果为5
函数名: strcspn
原 型: int strcspn(char *str1, char *str2);
printf("%d \n", strcspn("111cdef123", "cd"));//结果3
printf("%d \n", strcspn("1cdccc1def123", "cd")); //结果为1
函数名: strrchr
原 型: char *strrchr(char *str, char c);
程序例: printf("%s \n", strrchr("1cdccc1def123",'d')); 结果为 def123
程序例: printf("%s \n", strrchr("1cdccc1def123d",'1')); 结果为123d
函数名: strpbrk
原 型: char *strpbrk(char *str1, char *str2);
程序例: printf("%s \n", strpbrk("1cdccc1def123d","fe")); //结果为ef123d
函数名: strtok
原 型: char *strtok(char *str1, const char *str2);
程序例:
char s1[]="d+1cdccc1*def1,23,dad";
printf("%s \n", strtok(s1,"*"));
printf("%s \n",s1);
结果为:
d+1cdccc1
d+1cdccc1
函数名: strlen
原 型: char * ch
- int my_strlen(char* ch)
- {
- int i = 0;
- while (ch[i] != '\0')
- {
- i++;
- }
- return i;
- }
- int main()
- {
- char ch[] = "hello world";
- int len = my_strlen(ch);
- printf("%d \n", len);
- return EXIT_SUCCESS;
- }
复制代码
第二种写法
- int my_strlen(char* ch)
- {
- char* temp = ch;
- while (*temp != '\0')temp++;
- return temp - ch;
- }
复制代码
- char* my_strstr(char* str1, char* str2)
- {
- char* fstr = str1;
- char* rstr = str1;
- char* tstr = str2;
- while (*fstr)
- {
- rstr = fstr;
- while (*fstr == *tstr&&*fstr != '\0')
- {
- fstr++;
- tstr++;
- }
- if (*tstr == '\0')
- {
- return rstr;
- }
- tstr = str2;
- fstr = rstr;
- fstr++;
- }
- return NULL;
- }
- //字符串出现次数
- int main()
- {
- char*str = "11abcd222323abcd333abcd444qqq";
- char ch[] = "abcd";
- char* p = my_strstr(str, ch);
- int count = 0;
- while (p!=NULL)
- {
-
- count++;
- p = p + strlen(ch);
- p = my_strstr(p, ch);
- }
- printf("在字符串中出现%d次 \n", count);
- return EXIT_SUCCESS;
- }
复制代码
do-while
- //字符串出现次数
- int main()
- {
- char*str = "11abcd222323abcd333abcd444qqq";
- char ch[] = "abcd";
- char* p = my_strstr(str, ch);
- int count = 0;
-
- do
- {
- if (p)
- {
- count++;
- p += strlen(ch);
- p = my_strstr(p, ch);
- }
- } while (p);
- printf("%d \n", count);
- return EXIT_SUCCESS;
- }
复制代码 |
评分
-
查看全部评分
上一篇: C学习之路的点滴记录---基础指针数组--大牛飘过下一篇: c++习题求解
|