0

I wrote some code to print out the required number of letters inputted into a given text. For some reason after 9 letters it seems to increment by one and will slightly overshoot the number of letters given in the input. Any advice much appreciated :)

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

// Declaring the count letter function
int count_letter(string text);
int count_word(string word);
int count_sentence(string sentence);


int main(void)
{
    // Getting a string of Text and storing it in a variable
    string passage = get_string("text: ");
    {
        printf("%i letter(s)\n %i word(s)\n %i sentence(s)", count_letter(passage), count_word(passage), count_sentence(passage));

    }

}









int count_letter(string text)
{
    // Declaring function variables
    int lettercount = 0;
    int number_of_letters;
    int spaces = 0;
    int letter_all;


    // Getting length of the text
    number_of_letters = strlen(text);

    // Counting the letters in the text inputted
    for(lettercount = 0; lettercount < number_of_letters; lettercount++)
       {    // If text is from a-z then count the text
            if(isalpha(text[lettercount]))
                lettercount++;
            // If text is equal to space then add up the spaces
            else if(isspace(text[lettercount]))
                spaces++;
            // Minus the spaces from the lettercount
            letter_all = lettercount - spaces;
        }

    return letter_all;

}



int count_word(string word)
{
   // Declaring the function variables
   int wordcount = 1;
   int number_of_letters;
   int spaces = 0;
   int words_all;

   // Getting length of the text
    number_of_letters = strlen(word);

    // Counting the letters in the text inputted
    for(int lettercount = 0; lettercount < number_of_letters; lettercount++)
       {
            // If text is equal to space then add up the spaces
            if(isspace(word[lettercount]))
                spaces++;
            // Create variable called words all
            words_all = spaces + 1;
        }

    return words_all;

}




int count_sentence(string sentence)
{
    int number_of_letters;
    int number_of_sentences = 0;
     int lettercount;
    // Getting length of the text
    number_of_letters = strlen(sentence);

    for(lettercount = 0; lettercount < number_of_letters; lettercount++)
        if(sentence[lettercount] == 33 || sentence[lettercount] == 46 || sentence[lettercount] == 63)
            number_of_sentences++;

    return number_of_sentences;

}

1
int count_letter(string text)
{
    // Declaring function variable
    int lettercount = 0;

    // Looping over the letters in the text inputted
    for(int index = 0, length = strlen(text); index < length; index++)
       {    // If the character is a letter
            if(isalpha(text[index]))
                // Increment lettercount
                lettercount++;
        }
    return lettercount;
}
  • Thanks for the help. Just tried that but doesn't seem to give me the right output. Isn't that essentially the same code except replacing the variable names with index and length? – Andrewoca Apr 1 '20 at 15:19
  • Yeah, but also removing the part where you remove the spaces and just counting uppercase and lowercase letters. It works fine. – xemeds Apr 1 '20 at 15:36
  • understood man thanks for the suggestion! – Andrewoca Apr 1 '20 at 18:56

You must log in to answer this question.

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