Skip to content
Related Articles
Open in App
Not now

Related Articles

ispunct() function in C

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 16 Dec, 2021
Improve Article
Save Article

The ispunct() function checks whether a character is a punctuation character or not.
The term “punctuation” as defined by this function includes all printable characters that are neither alphanumeric nor a space. For example ‘@’, ‘$’, etc.
This function is defined in ctype.h header file.

syntax:

int ispunct(int ch);
ch: character to be checked.
Return Value  : function return nonzero
 if character is a punctuation character;
 otherwise zero is returned. 

CPP




// Program to check punctuation
#include <stdio.h>
#include <ctype.h>
int main()
{
    // The punctuations 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;
}



Output:


Sentence contains 2 punctuation characters.

CPP




// 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;
}



Output:

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

This article is contributed by Shivani Ghughtyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!