1
votes

I am trying to code a program that count the number of letters, words, and sentences in a text. I may assume that a letter is any lowercase character from a to z or any uppercase character from A to Z, any sequence of characters separated by spaces should count as a word, and that any occurrence of a period, exclamation point, or question mark indicates the end of a sentence.

So far, I could count both the number of letters and sentences correctly, but I miss out on the number of words:

e.g. yes!

The output should be: 3 letter(s) 1 word(s) 1 sentence(s)

What I get is: 3 letter(s) 0 word(s) 1 sentence(s)

UPDATE: It works fine now after typing out another (words++) in the end right before the printf function. Thanks for the help guys :).

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

int main(void)
{
    string text = get_string("Enter text: ");
    printf("Output:\n");
    int lettercount;
    int words = 0;
    int sentences = 0;
    int letters = 0;
    int length = strlen(text);
    for(lettercount = 0; lettercount < length; lettercount++)
    {
        if(isalpha(text[lettercount]))
        {
            letters++;
        }
        else if(text[lettercount] == ' ' || text[lettercount] == '\t' || text[lettercount] == '\n' || text[lettercount] == '\0')
        {
            words++;
        }
        else if(text[lettercount] == '.' || text[lettercount] == '!' || text[lettercount] == '?')
        {
            sentences++;
        }
    }
    words++;
    printf("%i letter(s)\n", letters);
    printf("%i word(s)\n", words);
    printf("%i sentence(s)\n", sentences);
}
2
Have you tested and debugged with shorter sentences? Reduce the test data to be as simple as possible and that still reproduces the problem (e.g two words). Then use a debugger and/or debug print statements to trace the program execution.kaylum
Hint: text[lettercount] == '\0' is always false. Also hint: strlen is O(n) in the length of the string, don't call it each time through your loop.Thomas
I think your problem can be reproduced even with a single word sentence like: Yes!kaylum

2 Answers

1
votes

You will always have words -1 because you only add new word to your counter after space or new line but what about the last word !? always the last word will not be counted so after counting any paragraph add 1 to your word counter .. Ex : Yes! --> 3 letters 1 Sentence 0 word ! so you add one and solved another Ex : Hello World! --> 10 letters 1 sentence 1 word ! adding one and it's solved

2
votes

The main problem with your code is that it does not count any 'final' word in your input text if that does not have a space after it (the terminating '\0' character will not be part of the tested string, as the strlen function does not include that.

Further, you will have problems if you have words that are separated by more than one space; to address this, you could use an inWord flag to keep track of if the current character is already inside a word and, if not, set that flag whenever we find a letter.

Also, your sentence count will be problematical if you have things like "..." in your input; the commented-out line after your sentences++; line will fix that (if you want to).

And, finally, just to be precise, you should not assume that the letters "a" thru "z" and "A" thru "Z" will be in a continuous sequence. They probably will be (most systems these days use ASCII encoding) but you should use the isalpha function for more portability (and the isspace function, too).

int main(void)
{
    string text = get_string("Enter text: ");
    printf("Output:\n");
    int lettercount;
    int words = 0;
    int sentences = 0;
    int letters = 0;
    int inWord = 0;// Set to 1 if we are inside a (new) word!
    int length = (int)(strlen(text)); // Don't evaluate length on each loop!
    for (lettercount = 0; lettercount < length; lettercount++) {
        int testChar = text[lettercount]; // Get a local copy of the current character
        if (isalpha(testChar)) { // Don't assume that 'a' ... 'z' and 'A' ... 'Z' are in contiguous sequences
            letters++;
            if (!inWord) words++; // Any letter means that we're in a (possibly new) word...
            inWord = 1;           // ... but now set this 'flag' so as not to count others!
        }
        else if (testChar == '.' || testChar == '!' || testChar == '?') {
            sentences++;
        //  if (inWord) sentences++; // Check that we're in a word, or stuff like "..." will be wrong
            inWord = 0; // Now we are no longer inside our current word
        }
        else if (isspace(testChar)) { // We could also just assume ANY other character is a non-word
            inWord = 0; // Now we are no longer inside our current word
        }
    }
    printf("%i letter(s)\n", letters);
    printf("%i word(s)\n", words);
    printf("%i sentence(s)\n", sentences);
    return 0;
}

Feel free to ask for any further clarification and/or explanation.