
2条回答 默认 最新
qfl_sdu 2021-06-17 17:31关注要求没看明白,是查找字符串1中有多少个字符串2吗?
这样的话代码如下:
#include <stdio.h> #include <string.h> #include <ctype.h> //查找big串中所有smal串的位置,并把位置存放在pos中,nmb存放个数 void findstr(char*big ,char* smal,int pos[],int *nmb) { int i,j,lenb,lens; lenb = strlen(big); lens = strlen(smal); *nmb = 0; if(lens > lenb) return; i = 0; while(i < lenb-lens+1) { for (j = 0; j < lens;j++) { if(tolower(big[i+j]) != tolower(smal[j])) break; } if (j == lens) //说明找到 { pos[*nmb] = i; (*nmb)++; i += lens; }else i++; } } int main() { char buf1[100] ={0}; char buf2[100] = {0}; int pos[20]; int nmb = 0; printf("请输入两个字符串:"); gets(buf1); gets(buf2); findstr(buf1,buf2,pos,&nmb); printf("%s中含有%s的个数为%d\n",buf1,buf2,nmb); return 0; }本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报