02 | C找到字符串中字串出现的次数
#include <stdio.h> #include <string.h> int countStr(char* str, char* substr) { char *p = strstr(str, substr); //问题的关键在这个函数,会返回第一个匹配到的位置 int count = 0; while (p != NULL) { count++; p += strlen(substr); //截取 p = strstr(p, substr); } return count; } int main() { char a[] = "uiosdfzdf"; char b[] = "zd"; int result = countStr(a, b); printf("字串出现了%d次\n", result); getchar(); return 0; }