下面的代码从stdin
逐个字符地传输,直到遇到 EOF 符号 (CTRL-D)。但是,当我执行 CTRL-D 命令时,它不会将其处理为 EOF 字符。
#include <stdio.h>
#include <ctype.h>
int main() {
char current_character, next_character;
int amount_of_characters = 0, amount_of_words = 0, amount_of_newlines = 0;
while( (current_character = getchar()) != EOF) {
amount_of_characters++;
if(isspace(current_character) || current_character == '\n') {
next_character = getc(stdin);
if(isalpha(next_character)) {
amount_of_words++;
ungetc(next_character, stdin);
}
if(current_character == '\n') {
amount_of_newlines++;
}
}
}
printf("----- Summary -----\n");
printf("Characters: %d\nWords: %d\nNewlines: %d\n", amount_of_characters, amount_of_words, amount_of_newlines);
return 0;
}
None
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)