In this source code example, we will see how to use the isspace() function in C programming with an example.
isspace() Function Overview
The isspace() function is provided by the <ctype.h> library in C. This function is designed to check if a given character is a whitespace character. Common whitespace characters include spaces (' '), newlines ('\n'), horizontal tabs ('\t'), vertical tabs ('\v'), form feeds ('\f'), and carriage returns ('\r').
Key Points:
- The function requires the inclusion of the <ctype.h> header.
- It takes an int (representing the ASCII value of the character) as its argument.
- Returns a non-zero integer if the character is whitespace, otherwise it returns 0.
Source Code Example
#include <stdio.h>
#include <ctype.h> // Required for isspace()
int main() {
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
if (isspace(ch)) {
printf("The character you entered is a whitespace character.\n");
} else {
printf("The character you entered is not a whitespace character.\n");
}
return 0;
}
Output
Enter a character: The character you entered is a whitespace character. or Enter a character: A The character you entered is not a whitespace character.
Explanation
1. At the beginning, required header files are included: stdio.h for standard I/O operations and ctype.h for the isspace() function.
2. In the main() function, a character variable ch is declared.
3. The user is prompted to input a character, which is stored in the ch variable.
4. The isspace() function is used to verify if the given character is a whitespace character.
5. A message is displayed to the user based on whether the character is a whitespace or not.
Comments
Post a Comment