Search code examples
ccs50

How to count the number of sentences in a given text in C?


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

int count_sentences(string text);

int main(void)
{
    string text = get_string("Text: ");
    int count_sentences(string text);
    {
         int sentences = 1;
         for (int j = 0; j < strlen(text); j++)
         {
              if (text[j] == '.' || text[j] == '?' || text[j] == '!')
              {
                 sentences++;
              }
         }
         return sentences;
    }
    printf("Sentences: %i\n", count_sentences);
}

I am doing the cs50 course through edx. I am stuck on a problem set in week two that has us write a program to assign a reading grade level to a user given text. I have figured out how to count the letters and words portion of the problem. However, the sentence count portion is giving me a hard time. Above is the test piece I have drafted before I implement it in my final code to make sure it functions the way I want. When I try to make the code it gives me this error:

test.c:24:31: error: format specifies type 'int' but the argument has type 'int (*)(string)' (aka 'int (*)(char *)') [-Werror,-Wformat]
   24 |     printf("Sentences: %i\n", count_sentences);
      |                        ~~     ^~~~~~~~~~~~~~~
1 error generated.
make: *** [<builtin>: test] Error 1

I cannot figure out what I am doing wrong. Any advice would be greatly appreciated.


Solution

  • You have multiple issues.

    • You are trying to nest the count_sentences function inside main. Don't do this. Some compilers might allow it as a specific extension, but there's no need at this stage of your learning career to rely on those.
    • You've only declared that function because of the semicolon. The block following int count_sentences(string text); is always executed. That means that return makes your print unreachable.
    • You haven't called count_sentences in your call to printf. Even if you do, you'd have to provide a string (alias for char *) argument to make it a correct call.
    • It's comparatively minor, but you have extraneous includes.

    If we correct these issues:

    #include <cs50.h>
    #include <stdio.h>
    #include <string.h>
    
    int count_sentences(string text);
    
    int main(void)
    {
        string text = get_string("Text: ");
        printf("Sentences: %i\n", count_sentences(text));
    }
    
    int count_sentences(string text) 
    {
        int sentences = 1;
        for (int j = 0; j < strlen(text); j++)
        {
            if (text[j] == '.' || text[j] == '?' || text[j] == '!')
            {
                sentences++;
            }
        }
        return sentences;
    }
    

    There remains the algorithmic issue of whether you're actually counting sentences properly. Your code may need to be a bit more sophisticated to detect things like an input string "???" which would tell us there are four sentences.

    Note that when the compiler error tells you the argument is of type int (*)(char *) be aware that this is a function pointer type. In this case a pointer to a function which takes a char * and returns an int. That's exactly what count_sentences is. When that bare function name is passed to a function it decays to a pointer to the function.