字符检测:C语言ispunct()函数--判断字符是否为标点符号或特殊字符

来源:互联网 发布:国产口罩品牌知乎 编辑:程序博客网 时间:2024/06/06 01:41
ispunct() 函数用来检测一个字符是否为标点符号或特殊字符,其原型为:
    int ispunct(int c);

【参数】c 为需要检测的字符。

【返回值】若 c 为标点符号或特殊符号(非空格、非数字和非英文字母)返回非 0 值,否则返回 0。

注意,此为宏定义,非真正函数。

【实例】列出字符串str 中的标点符号或特殊符号。

#include <stdio.h>#include <ctype.h>int main (){    int i=0;    int cx=0;    char str[]="Hello, welcome!";    while (str[i])    {        if (ispunct(str[i])) cx++;        i++;    }    printf ("Sentence contains %d punctuation characters.\n", cx);    return 0;}
输出结果:
Sentence contains 2 punctuation characters.

0 0
原创粉丝点击