Task: Read a line and count transitions from non-word to word characters (letters or digits) to handle multiple spaces. Print the total word count.
#include <stdio.h>
#include <ctype.h>
int main(void) {
char s[512];
int c, i=0, in_word=0, words=0;
printf("Enter a sentence: ");
while ((c=getchar())!='\n' && c!=EOF) { if(i<511) s[i++]=(char)c; }
s[i]='\0';
for (i=0; s[i]; ++i) {
if (isalnum((unsigned char)s[i])) {
if (!in_word) { in_word = 1; ++words; }
} else {
in_word = 0;
}
}
printf("Word count = %d\n", words);
return 0;
}