Bonus addition to Salem's testing code: store the expecte number of words along with the test sentences, and have the test code compare and print results for you:
Code:
struct words_test {
char *sentence;
int expected_words;
};
struct words_test tests[] = {
{"", 0},
{"word", 1},
...
{"word word word", 3}
};
// in the loop
int num_words = count_words(tests[i].sentence)
if (num_words != tests[i].expected_words)
printf("ERROR: >>%s<< expected %d words, got %d\n", tests[i].sentence, tests[i].expected_words, num_words);
else
printf("SUCCESS: >>%s<< has %d words\n", tests[i].sentence, num_words);