ispunct() Function
The ispunct()
function in C is used to determine if a character is a punctuation character. It checks whether a character falls within the set of graphical characters that are not alphanumeric. This function is particularly useful for parsing text and filtering out punctuation marks in data processing tasks.
Syntax of ispunct()
int ispunct(int c);
Parameters
Parameter | Description |
---|---|
c | An integer representing the character to be checked, typically cast from a char or EOF. |
In the default “C” locale, ispunct()
considers all graphic characters that are not alphanumeric as punctuation. Note that other locales might define a different set of characters as punctuation, but the common property is that they are graphical and not letters or digits.
Return Value
The function returns a nonzero value (true) if the character is a punctuation character, and zero (false) otherwise.
Examples for ispunct()
Example 1: Basic Check for a Punctuation Character
This example demonstrates how to use ispunct()
to check a single character for punctuation.
Program
#include <stdio.h>
#include <ctype.h>
int main() {
int ch = '!';
if (ispunct(ch)) {
printf("The character %c is a punctuation character.\n", ch);
} else {
printf("The character %c is not a punctuation character.\n", ch);
}
return 0;
}
Explanation:
- A character is assigned the value
'!'
. - The
ispunct()
function checks if'!'
is a punctuation character. - Since
'!'
is considered punctuation, the program prints a confirmation message.
Program Output:
The character ! is a punctuation character.
Example 2: Checking Multiple Characters for Punctuation
This example iterates through a string and uses ispunct()
to identify punctuation characters among a mix of characters.
Program
#include <stdio.h>
#include <ctype.h>
int main() {
char testStr[] = "Hello, World!";
int i = 0;
while (testStr[i] != '\0') {
if (ispunct(testStr[i])) {
printf("'%c' is a punctuation character.\n", testStr[i]);
} else {
printf("'%c' is not a punctuation character.\n", testStr[i]);
}
i++;
}
return 0;
}
Explanation:
- A string
"Hello, World!"
is defined. - The program iterates over each character in the string.
- For each character,
ispunct()
determines if it is a punctuation character. - A message is printed indicating whether each character is punctuation or not.
Program Output:
'H' is not a punctuation character.
'e' is not a punctuation character.
'l' is not a punctuation character.
'l' is not a punctuation character.
'o' is not a punctuation character.
',' is a punctuation character.
' ' is not a punctuation character.
'W' is not a punctuation character.
'o' is not a punctuation character.
'r' is not a punctuation character.
'l' is not a punctuation character.
'd' is not a punctuation character.
'!' is a punctuation character.
Example 3: Filtering Out Punctuation from a Sentence
This example demonstrates how to process an entire sentence and filter out all punctuation characters.
Program
#include <stdio.h>
#include <ctype.h>
int main() {
char sentence[] = "Good morning, world!";
int i = 0;
printf("Sentence without punctuation: ");
while (sentence[i] != '\0') {
if (!ispunct(sentence[i])) {
printf("%c", sentence[i]);
}
i++;
}
printf("\n");
return 0;
}
Explanation:
- A sentence
"Good morning, world!"
is defined. - The program iterates over each character of the sentence.
- The
ispunct()
function is used to check if a character is punctuation. - If the character is not punctuation, it is printed; punctuation characters are skipped.
Program Output:
Sentence without punctuation: Good morning world