>>Any other tips?
To add to whats already been said: Yes, your indentation is still wrong. The following is your code tidied up (no changes, just the layout)
Code:
#include <stdio.h>
#include <ctype.h>
int read_and_count(void);
int chr;
int word_count = 0;
int next_word = 0;
int letter_count = 0;
int main(void)
{
printf("Please type a sentence\n");
read_and_count();
return(0);
}
int read_and_count(void)
{
while (chr != EOF && !isspace(chr) && chr != '.')
{
letter_count = (letter_count + 1);
next_word++;
chr = getchar();
putchar(chr);
}
if (chr == '.')
{
letter_count = (letter_count - 1);
printf("%10d characters long\n", letter_count);
printf("Words : %d\n", word_count);
printf("Letters : %d\n", letter_count);
return(0);
}
else if (chr == EOF)
{
printf("%10d characters long", letter_count);
return(0);
}
else if (chr == isspace(chr))
{
return(1);
}
return(1);
}
>>while (chr != EOF && !isspace(chr) && chr != '.')
The first time you go through this loop, you haven't actually read anything into chr. Personally, I think I'd have controlled the loop like this:
Code:
while ((c = getchar()) != EOF)
{
/* count words and letters here */
}
Either that, or use fgets() to grab a buffer load of data first. Either way, it's up to you to do what you're comfortable with.