isspace() Function
The isspace()
function in C is used to determine if a character is a white-space character. It checks if the provided character is one of the standard white-space characters defined by the locale. This function is essential in text processing where filtering or identifying space characters is required.
Syntax of isspace()
int isspace(int c);
Parameters
Parameter | Description |
---|---|
c | An integer representing the character to be checked, typically cast from a char or EOF. |
It is worth noting that for the default “C” locale, the white-space characters include space (' '
), horizontal tab ('\t'
), newline ('\n'
), vertical tab ('\v'
), form feed ('\f'
), and carriage return ('\r'
). Other locales may define additional characters as white-space, but none will classify an alphanumeric character as white-space.
Return Value
The function returns a nonzero value (true) if the character is a white-space character, and zero (false) otherwise.
Examples for isspace()
Example 1: Checking a Single White-space Character
This example demonstrates the basic usage of isspace()
by checking if a single character is a white-space.
Program
#include <stdio.h>
#include <ctype.h>
int main() {
int ch = ' ';
if (isspace(ch)) {
printf("The character ' ' is a white-space character.\n");
} else {
printf("The character ' ' is not a white-space character.\n");
}
return 0;
}
Explanation:
- A character variable is assigned the space character (
' '
). - The
isspace()
function checks if this character is a white-space. - Since a space is a white-space, the program prints that it is a white-space character.
Program Output:
The character ' ' is a white-space character.
Example 2: Differentiating White-space from Non White-space Characters
This example checks multiple characters to identify which ones are white-space characters and which ones are not.
Program
#include <stdio.h>
#include <ctype.h>
int main() {
char testChars[] = " \tA\nB";
int i = 0;
while (testChars[i] != '\0') {
if (isspace(testChars[i])) {
printf("'%c' is a white-space character.\n", testChars[i]);
} else {
printf("'%c' is not a white-space character.\n", testChars[i]);
}
i++;
}
return 0;
}
Explanation:
- An array of characters is initialized with a space, a horizontal tab, letters, and a newline.
- The program iterates through each character in the array.
- For each character,
isspace()
is used to check if it is a white-space character. - The program prints the result for each character.
Program Output:
' ' is a white-space character.
' ' is a white-space character.
'A' is not a white-space character.
'
' is a white-space character.
'B' is not a white-space character.
Example 3: Scanning a String for White-space Characters
This example demonstrates how to scan an entire string to check each character for white-space using isspace()
.
Program
#include <stdio.h>
#include <ctype.h>
int main() {
char str[] = "Hello World!\n";
int i = 0;
while (str[i] != '\0') {
if (isspace(str[i])) {
printf("Character '%c' is a white-space character.\n", str[i]);
} else {
printf("Character '%c' is not a white-space character.\n", str[i]);
}
i++;
}
return 0;
}
Explanation:
- A string containing letters, a space, and a newline is defined.
- The program iterates through the string character by character.
- The
isspace()
function checks each character to determine if it is a white-space. - A message is printed indicating whether each character is a white-space character or not.
Program Output:
Character 'H' is not a white-space character.
Character 'e' is not a white-space character.
Character 'l' is not a white-space character.
Character 'l' is not a white-space character.
Character 'o' is not a white-space character.
Character ' ' is a white-space character.
Character 'W' is not a white-space character.
Character 'o' is not a white-space character.
Character 'r' is not a white-space character.
Character 'l' is not a white-space character.
Character 'd' is not a white-space character.
Character '!' is not a white-space character.
Character '
' is a white-space character.