I'm trying to write a program which counts the number of words found in a string, however the program which I wrote counts the number of spaces. How do I construct this program - as efficient as possible - to count the number of words? What do I do say the string contains 4 words and 8 spaces, or if it contains no words and only spaces? What am I missing/doing wrong?
#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <string.h>
int main(void)
{
string text = get_string("Text: ");
int count_words = 0;
for (int i = 0, n = strlen(text); i < n; i++)
{
if (isspace (text[i]))
{
count_words++;
}
}
printf("%i\n", count_words + 1);
}