主要是应用了头文件<string.h>中的strstr函数 char * strstr(const char *s1, const char *s2);
查找是否存在:
1 #include<stdio.h> 2 #include<string.h> 3 int main(void) 4 { 5 char str3[] = {"abcabcdababc"}; 6 char str4[] = "abc",*b; 7 int count=0; 8 int len = strlen(str4); 9 b = strstr(str3,str4); 10 if(b) 11 printf("find!\n"); 12 else 13 printf("not find\n"); 14 return 0; 15 }
计算出现的个数:
1 #include<stdio.h> 2 #include<string.h> 3 int main(void) 4 { 5 char str3[] = {"abcabcdababc"}; 6 char str4[] = "abc",*b; 7 int count=0; 8 int len = strlen(str4); 9 b = strstr(str3,str4); 10 while(b) 11 { 12 count++; 13 b = strstr(b+len,str4); 14 } 15 printf("%d\n",count); 16 17 return 0; 18 }
时间: 2024-11-05 08:24:44