如何只计算C语言中的一个句点?

use*_*055 -1 c c-strings counting char

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

我怎样才能计算一个周期?

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

Chr*_*ris 5

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

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

    for (size_t i = 0; s[i]; i++) {
        if (s[i] == '.') count++;
    }

    return count;
}
Run Code Online (Sandbox Code Playgroud)