Home > Enterprise >  Why doesn't the program print "letters" if i specified that i want to use it?
Why doesn't the program print "letters" if i specified that i want to use it?

Time:11-06

I need to print out the amount of letters, sentences and words input on the terminal when I run the program. I tried to do it this way but I'm surely doing something wrong.

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

int count_letters(string text);
int count_words(string text);
int count_sentences(string text);

int main(void)
{
    //Ask the user for input
    string text = get_string("Type paragraph here: ");
    //Print the inputed text on to the console
    printf("Text: %s\n", text);
}

int count_letters(string text)
{
    int letters = 0;

    for (int i = 0; i < strlen(text); i  )
    {
        if (isalpha(text[i]))
        {
            letters  ;
        }
    }
    return letters;

    printf("%i letters", letters);
}

int count_words(string text)
{
    int words = 1;

    for (int i = 0; i < strlen(text); i  )
    {
        if (text[i] == ' ')
        {
            words  ;
        }
    }
    return words;
}

int count_sentences(string text)
{
    int sentences = 0;

    for (int i = 0; i < strlen(text); i  )
    {
        if (text[i] == '.' || text[i] == '!' || text[i] == '?')
        {
            sentences  ;
        }
    }
    return sentences;
}

CodePudding user response:

You have never called any of the functions you've written in main. Only code in main is executed.

You might:

int main(void)
{
    //Ask the user for input
    string text = get_string("Type paragraph here: ");
    //Print the inputed text on to the console
    printf("Text: %s\n", text);

    printf("Number of letters: %d\n", letters(text));
}
  • Related