Everybody,
Here's my simplistic version of a wc command, a version that counts a text file's line of text, even when there's no '\n' on that line. I just can't figure out to revise the program, so it won't need "in_word = !isspace(line)". Any suggestions? Thanks.
Here's my simplistic version of a wc command, a version that counts a text file's line of text, even when there's no '\n' on that line. I just can't figure out to revise the program, so it won't need "in_word = !isspace(line)". Any suggestions? Thanks.
Code:
int
main(void)
{
FILE *input;
register int i = 0, characters = 0, lines = 0, words = 0;
char line[MAX + 1];
bool in_word;
if (!(input = fopen("words.txt", "r")))
perror("mywc");
else
{
while (fgets(line, MAX, input) != NULL)
{
++lines;
for (i = 0; line[i] != '\0'; ++i)
{
in_word = !isspace(line[i]);
if (in_word && isspace(line[i + 1]))
++words;
}
characters += i;
}
if (in_word)
++words;
}
printf("%d characters\n %d words\n%d lines\n", characters, words, lines);
return 0;
}