Как получить мой код для печати правильного количества букв? - PullRequest
0 голосов
/ 01 апреля 2020

Я написал код для распечатки необходимого количества букв, введенных в данный текст. По какой-то причине после 9 букв он, по-видимому, увеличивается на единицу и будет слегка превышать количество букв, вводимых во входные данные. Любой совет высоко ценится:)

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

// Function to count letters
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 main(void)
{
    // Getting a string of Text and storing it in a variable
    string passage = get_string("text: ");
    {
        printf("%i letter(s)\n", count_letter(passage));

    }

}

Ответы [ 2 ]

2 голосов
/ 03 мая 2020

в вашей функции вы должны удалить эту строку letter_all = lettercount - пробелы, которые уменьшат количество реальных букв

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

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

    // Getting length of the text
    number_of_letters = strlen(text);
    // Counting the letters in the text inputted
    for(i = 0; i < number_of_letters; i++)
       {    // If text is from a-z then count the text
            if(isalpha(text[i]))
                lettercount++;
        }

    return lettercount;

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

    }

}
2 голосов
/ 01 апреля 2020

Когда вы делаете letter_all = lettercount - spaces, вы вычитаете количество пробелов из количества букв. Итак, если у вас есть строка «he llo», у вас есть 5 букв и 1 пробел, и вы делаете 5-1. тогда ваша программа напечатает 4, что неверно. Итак, вам нужно просто напечатать буквенный подсчет, чтобы получить номер букв.

Вот как должна быть ваша функция.

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

    // Getting length of the text
    number_of_letters = strlen(text);
    // Counting the letters in the text inputted
    for(i = 0; i < number_of_letters; i++)
       {    // If text is from a-z then count the text
            if(isalpha(text[i]))
                lettercount++;
        }

    return lettercount;

}
...