【注:本程序验证是使用vs2013版】
#include <stdio.h> #include <stdlib.h> #include <string.h> #pragma warning(disable:4996) int my_strAppoints(char *src, char *Specify, char *n){ char *from = src; char *equal = Specify; int frequency = 0; if (*from == 0){ return -1; } while (1){ from = strstr(from, equal);//strstr():查询 指定内容在内存中出现的首地址,并返回该首地址 if (from != NULL){ from = from + strlen(equal);//重新设置起点的位置 frequency++; } else{//如果到结束符 *n = frequency; return 0; } } } int main(void){ char *p = "11abcd1235abcd++5abcd5+53414"; int times = 0; //字符串出现的次数 int ret = 0; ret = my_strAppoints(p, "abcd", ×); //查询abcd 在p中出现的次数,这里次数需要传递地址 if (ret != 0){ printf("my_strAppoints err:%d\n", ret); return; } printf("times = %d\n", times);
printf("\n"); system("pause"); return 0; }
原文:https://www.cnblogs.com/wlstm/p/11105491.html