In this source code example, we will see how to use the ispunct() function in C programming with an example.
ispunct() Function Overview
The ispunct() function is a part of the <ctype.h> library in C. It is used to check if a given character is a punctuation character, which includes symbols like !, @, #, $, etc., but not alphanumeric characters or whitespace.
Key Points:
- To use the ispunct() function, the <ctype.h> header must be included.
- The function accepts an int as an argument, which represents the ASCII value of the character.
- It returns a non-zero integer if the character is a punctuation character, otherwise, it returns 0.
Source Code Example
#include <stdio.h>
#include <ctype.h> // Required for ispunct()
int main() {
char ch;
printf("Enter a character: ");
scanf("%c", &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;
}
Output
Enter a character: ! The character '!' is a punctuation character. Enter a character: A The character 'A' is not a punctuation character.
Explanation
1. The necessary header files are included: stdio.h for standard input/output functions and ctype.h for the ispunct() function.
2. Within the main() function, a character variable ch is declared.
3. The user is then prompted to input a character, which is stored in the ch variable.
4. Using the ispunct() function, the program checks if the provided character is a punctuation character.
5. Depending on the result, a corresponding message is displayed to the console, indicating if the character is a punctuation character or not.
Comments
Post a Comment