用c语言任意输入一段100词左右的英文,输出本段英文实际的单词数、符号数及具体有多少句话。(提升:可考虑宇母使用的频次,每种符号的使用次数
时间: 2024-03-12 14:45:17 浏览: 10
好的,我可以帮你解决这个问题。以下是一个示例程序,可以实现你的需求:
```c
#include <stdio.h>
#include <ctype.h>
#define MAX_WORDS 100
int main() {
char text[MAX_WORDS];
int word_count = 0;
int symbol_count = 0;
int sentence_count = 0;
int letter_count[26] = {0};
int digit_count = 0;
int space_count = 0;
int other_count = 0;
printf("请输入一段英文(100个单词以内):\n");
fgets(text, MAX_WORDS, stdin);
for (int i = 0; text[i] != '\0'; i++) {
char c = text[i];
if (isalpha(c)) {
word_count++;
letter_count[tolower(c) - 'a']++;
} else if (isdigit(c)) {
symbol_count++;
digit_count++;
} else if (isspace(c)) {
symbol_count++;
space_count++;
} else if (ispunct(c)) {
symbol_count++;
if (c == '.' || c == '?' || c == '!') {
sentence_count++;
}
} else {
other_count++;
}
}
printf("单词数:%d\n", word_count);
printf("符号数:%d\n", symbol_count);
printf("句子数:%d\n", sentence_count);
printf("字母使用频次:\n");
for (int i = 0; i < 26; i++) {
printf("%c: %d\n", 'a' + i, letter_count[i]);
}
printf("数字个数:%d\n", digit_count);
printf("空格个数:%d\n", space_count);
printf("其他符号个数:%d\n", other_count);
return 0;
}
```
这个程序使用了一些 C 语言的标准库函数和字符操作函数来实现你的需求。具体来说,它使用了 `fgets` 函数来读取用户输入的一行文本,然后逐个字符地遍历这个文本,统计单词数、符号数、句子数、字母频次、数字个数、空格个数和其他符号个数。
注意,这个程序假设用户输入的英文文本中只包含 ASCII 码中的可打印字符,不包含 Unicode 字符或其他非 ASCII 字符。如果你需要处理更复杂的文本,可能需要使用更高级的文本处理工具或库。