如何只计算C中的一个句号?

问题描述 投票:0回答:2

我只需要计算字符串中的句点 (.)。为此,我使用 ctype 库中的 ispunct 函数。我不想计算逗号 (,),也不想计算感叹号 (!)。

如何计算一个周期?

当我使用 ctype 库中的 ispunct 函数时,我的程序会计算句号 (.)、逗号 (,) 和感叹号。

c period
2个回答
1
投票

如果

ispunct
不能满足您的需要,请不要使用它。只需检查是否与
'.'
相等。

size_t count_periods(char *s) {
    size_t count = 0;

    for (; *s; s++) {
        if (*s == '.') count++;
    }

    return count;
}

0
投票

自己写

isdot

int isdot(char c) {
    return c == '.';
}
© www.soinside.com 2019 - 2024. All rights reserved.