我只需要计算字符串中的句点 (.)。为此,我使用 ctype 库中的 ispunct 函数。我不想计算逗号 (,),也不想计算感叹号 (!)。
如何计算仅一个周期?
当我使用 ctype 库中的 ispunct 函数时,我的程序会计算句号 (.)、逗号 (,) 和感叹号。
如果
ispunct
不能满足您的需要,请不要使用它。只需检查是否与 '.'
相等。
size_t count_periods(char *s) {
size_t count = 0;
for (; *s; s++) {
if (*s == '.') count++;
}
return count;
}
自己写
isdot
:
int isdot(char c) {
return c == '.';
}