當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。