include <stdio.h> #include <stdlib.h> int countchar(char *str1, char *str2) { int n = 0; int i = 0; while (*(str1 + i) != '\0') { int j = 0; if (*(str1 + i) == *(str2 + j)) { i += 2; j += 2; if (*(str1 + i) == *(str2 + j)) n++; } i++; } return n; } int main() { char str1[2000], str2[2000]; int n; printf("请输入一个文本:\n"); gets(str1); printf("请输入一个词:\n"); gets(str2); n = countchar(str1, str2); printf("这个词:%s 在这个文本:%s 中出现了%d次\n", str2, str1, n); system("pause"); return EXIT_SUCCESS; }
如果需要更长的文本则需要把那个字符串加长。