CS50 PSET2 (удобочитаемость) - ошибка сегментации при подсчете количества слов - PullRequest
0 голосов
/ 11 ноября 2019

Почему возникает ошибка сегментации, когда я добавляю несколько слов в мой текстовый ввод? Похоже, что это происходит в моей функции getWordNumber.

Вот мои основные (void) и функции getWordNumber:

int main(void)
{
    string s = get_string("Text: ");
    int length = strlen(s);
    printf("Letters: %i\n", getLetterNumber(s));
    printf("Words: %i\n", getWordNumber(s));
    printf("Sentences: %i\n", getSentenceNumber(s));

}

Функция для подсчета слов:

int getWordNumber(string y) {
    int words = 0;
    for (int j = 0; j < strlen(y); j++) {
        if (isalnum(y[j]) != 0) {
            words++;
            do {
                j++;
            } while (isspace(y[j]) == 0);
        }
    }
    return words;
}

Вот так выглядит мой терминал:

~/ $ ./readability
Text: When he was nearly thirteen, my brother Jem got his arm badly broken at the elbow. When it healed, and Jem's fears
Letters: 89
Words: 22
Sentences: 1
~/ $ ./readability
Text: When he was nearly thirteen, my brother Jem got his arm badly broken at the elbow. When it healed, and Jem's fears of never being able       
Letters: 105
Segmentation fault
...