I'm writing a program that counts the number of words and the number of lines. I tried to count spaces and tabulators but it does not work. For example if the input is:
Hello..\n \tI love coding!\n Do you?
The output will be 2 lines 4 words
#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;
}