C语言统计出现次数最多的单词
时间: 2023-11-28 21:46:25 浏览: 175
以下是C语言统计出现次数最多的单词的代码示例:
```c
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX_WORD_LEN 100
int main() {
char str[1000], word[MAX_WORD_LEN], max_word[MAX_WORD_LEN];
int i, j, len, max_count = 0, count = 0;
printf("Enter a string: ");
fgets(str, 1000, stdin);
len = strlen(str);
for (i = 0; i < len; i++) {
if (isalnum(str[i])) {
word[count++] = tolower(str[i]);
} else if (count != 0) {
word[count] = '\0';
count = 0;
for (j = 0; j < len; j++) {
if (strcmp(word, max_word) == 0) {
max_count++;
break;
}
}
if (j == len) {
if (max_count == 0) {
strcpy(max_word, word);
max_count = 1;
} else if (strcmp(word, max_word) < 0) {
strcpy(max_word, word);
max_count = 1;
}
}
}
}
printf("Most frequent word is '%s' with frequency %d.\n", max_word, max_count);
return 0;
}
```
阅读全文