当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C语言 ispunct()用法及代码示例


ispunct()函数检查字符是否为标点符号。此函数定义的术语“punctuation”包括所有可打印字符,既不是字母数字字符也不是空格。例如“ @”,“ $”等。此函数在ctype.h头文件中定义。

用法:

int ispunct(int ch);
ch: character to be checked.
Return Value : function return nonzero
 if character is a punctuation character;
 otherwise zero is returned. 
// Program to check punctuation 
#include <stdio.h> 
#include <ctype.h> 
int main() 
{ 
    // The puncuations in str are '!' and ',' 
    char str[] = "welcome! to GeeksForGeeks, "; 
  
    int i = 0, count = 0; 
    while (str[i]) { 
        if (ispunct(str[i])) 
            count++; 
        i++; 
    } 
    printf("Sentence contains %d punctuation"
           " characters.\n", count); 
    return 0; 
}

输出:


Sentence contains 2 punctuation characters.
// C program to print all Punctuations 
#include <stdio.h> 
#include <ctype.h> 
int main() 
{ 
    int i; 
    printf("All punctuation characters in C"
            " programming are:\n"); 
    for (i = 0; i <= 255; ++i) 
        if (ispunct(i) != 0) 
            printf("%c ", i); 
    return 0; 
}

输出:


All punctuation characters in C programming are:
! " # $ % & ' ( ) * +, - . /:;  ? @ [ \ ] ^ _ ` { | } ~


相关用法


注:本文由纯净天空筛选整理自 ispunct() function in C。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。