CS50 PSET2(可读性)-计算字数时出现段错误

时间:2019-11-11 20:13:05

标签: c cs50

当我在文本输入中再添加几个单词时,为什么会出现分割错误?看来它发生在我的getWordNumber函数上。

这是我的main(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

0 个答案:

没有答案