C语言ispunct()函数:判断字符是否为标点符号
ispunct()函数
头文件和函数原型
#include <ctype.h>
int ispunct(int c);
描述
ispunct()
是C语言标准库中的一个函数,用于检查一个字符是否为标点符号;
标点符号是指在文本中用于标点、分隔或表示特定含义的符号,例如逗号、句号、问号、感叹号、引号、括号、分号等。
返回值
如果传入的字符参数是标点符号,则返回非0
值,否则返回0
;
示例代码
这个示例用于统计字符串中包含多少个标点符号;
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main() {
char str[] = "\"Believe you can, and you're halfway there.\"" \
" - Theodore Roosevelt";
size_t length = strlen(str);
unsigned count = 0;
for (int i = 0; i < length; i++) {
if (ispunct(str[i])) {
count++;
}
}
printf("%d\n", count);
}
程序运行结果
6