0
votes

Here are the instructions of an exercise that I have to solve for cs50 pset2 readability(copy pasted from the site):

  • Your program should count the number of letters, words, and sentences in the text. You 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.

These are not the complete instructions, just the part I have trouble with.

I figured out how to count the number of letters in the text, but I can't figure out how to count the words and sentences. I've tried googling it and using other external resources, but all that pops up is the answer to the problem, which, frankly, feels like cheating. This is my code:

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


int main (void)
{
    int letters = 0;
    string text = get_string("Text: ");
    int words = 0;

    for (int i = 0; i < strlen(text);i++)
    {
        if(isalpha(text[i]) != 0)
        {
        letters++;
        }
    }
    printf("Letters: %i\n", letters);
    


   for (int i = 0; i < strlen(text);i++)
   {
       if (isspace(text[i]) != 0)
       {
          if(isalpha (text[i] + 1) != 0) 
          {
              words++;
          }
       }
 
   }
   
   printf("Words: %i\n", words);
}

This code counts the correct number of letters but always types Words : 0. I haven't done the sentences part. Can I please have some help? If you show me the answer, can you explain why that's the answer?

1
Welcome to SO. You might show us your code. Then we can tell you were to adjust it.Gerhardh
Hint: If you found some letters and then you find something not a letter then chances are high that you just found the end of a word and the next letter is starting a new word.Gerhardh
IMHO this question would maybe be more appropriate at math.stackexchange.com than on Stack Overflow although programming context because advanced math is more needed than programming art here.RobertS supports Monica Cellio
And I'll make it simpler too. Thanks a million for helping me learn how to improve my questions and for the advice. I won't forget it.Lost in code
Actually, learning how to ask for help is an important and not trivial skill to learn as a programmer :)klutt

1 Answers

2
votes
for (int i = 0; i < strlen(text);i++)
{
    if (isspace(text[i]) != 0)
    {
       if(isalpha (text[i] + 1) != 0) 
       {
           words++;
       }
    }
}

There are some wrong things here. What you need to do is to realize that this program can be in one of two states. Either you are currently reading a word or not.

bool reading_word = false; // Flag
int words = 0;

for(int i=0; i<strlen(text); i++) {
    if(isspace(text[i]) {
        reading_word = false;
    }
    else if(isalpha(text[i])) {
        if(!reading_word) {
            reading_word = true;
            words++;
        }
    }
}

Plus, don't write if(isspace(text[i]) != 0). It returns a Boolean value, so it's basically meant to be read "if text[i] is a space", so just write if(isspace(text[i]))

Also, in your code isalpha (text[i] + 1) is completely wrong and makes no sense. Since this is homework, I leave it to you to find out why.

For the sentences, you can use a function like this:

int isdelim(char c)
{
    return c == '.' || c == '!' || c == '?';
}

and then use it in a similar manner as the loop for counting words.