0

So far I can print the number of letters without issue but when it comes to printing the number of words I cannot get it to accurately calculate. It only returns the value of one which I've predefined. I have two instances of letters being printed to test that the for loop is working. Here's my code:

#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <math.h>
#include <ctype.h>


int main()
{
    string textInput;
    char textStr[200];
    int len, i, j=0, words = 1, letters=0;

    //////////Ask for text////////////
    textInput = get_string("Text: \n");

    //////////Determine Letter Count////////////
    len = strlen(textInput);

    //////////Determine Word Count, Print result////////////
    for (i = 0; i < len; i++)
    {
        if (isalpha(textInput[j]))
        {
            letters++;
        }
        else if (textInput[j] == ' ')
        {
            words++;
        }

    }
    /////////Print Results/////////
    printf("Letters: %d\n", len);
    printf("Words: %d\n", words);
    printf("letters2: %d\n", letters);

}
0

I had to update the [j] to [i]. It wasn't wasn't incrementing as j :)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .