What I did here was to go through a line character by character. If the current character is a letter, then turn on the switch to 1. If it is not a letter turn off the switch to 0. When the switch switches from 0 to 1, then I increment the word count. After a line is processed, the code prints the word count. Doesn't work though. huhu.
#include "stdio.h"
#include "string.h"
int main() {
char line[100];
int word_count, i;
bool word_found;
while (fgets(line, 100, stdin)) {
for (i = 0, word_found = 0, word_count = 0; line[i]; i += 1)
if ((line[i] >= 65 && line[i] <= 90) || (line[i] >= 97 && line[i] <= 122)) {
if (word_found == 0) {
word_count += 1;
word_found = 1;
}
}
else {
word_found = 0;
}
printf("%d\n", word_count);
}
return 0;
}
No comments:
Post a Comment