我正在编写一个计算单词数和行数的程序。我试图计算空格和制表符,但不起作用。例如,如果输入为:
Hello..\n \tI love coding!\n Do you?
输出为2行4字
#include <stdio.h>
#include <stdlib.h>
#define INT_MAX (100000000)
int is_newline(int c) {
return (c =='\n');
}
int is_word(int c){
return (c == ' ' || c == '\t');
}
int main(void) {
int ch;
int num_words = 0;
int new_line = 0;
ch = getchar();
while (ch != EOF) {
if (is_word(ch)) {
num_words++;
}else if (is_newline(ch)) {
new_line++;
num_word++;
}
ch = getchar();
}
printf("The number of words is: %d", num_words);
printf("The number of line is: %d", new_line);
return 0;
}