//統計一個長度為2的字符串在另外一個字符串中出現的次數。
1 #include <conio.h> 2 #include <stdio.h> 3 #include <string.h> 4 #include <stdlib.h> 5 int fun(char *str, char *substr) 6 { 7 char *z, *c; 8 z = str; 9 c = substr; 10 int n=0; 11 while (*z!='\0') 12 { 13 if (*z== *c) 14 { 15 z++; c++; 16 if (*z == *c) 17 { 18 n++; 19 } 20 c--; 21 } 22 else 23 { 24 z++; 25 } 26 } 27 return n; 28 } 29 void main() 30 { 31 FILE *wf; 32 char str[81],substr[3]; 33 int n; 34 system("CLS"); 35 printf("輸入主字符串: "); 36 gets(str); 37 printf("輸入子字符串: "); 38 gets(substr); 39 puts(str); 40 puts(substr); 41 n=fun(str,substr); 42 printf("n=%d\n ",n); 43 /******************************/ 44 wf=fopen("out.dat","w"); 45 n=fun("asd asasdfg asd as zx67 asd mklo","as"); 46 fprintf(wf,"%d",n); 47 fclose(wf); 48 /*****************************/ 49 }