Check if a String Contains Only Whitespace Characters in C
To check if a string contains only whitespace characters in C, you can iterate through the string and verify that every character is a whitespace.
We can use various approaches such as the isspace()
function from <ctype.h>
or by manually comparing each character to common whitespace values.
Examples to Check if a String Contains Only Whitespace
1. Using isspace()
Function
In this example, we will use the standard library function isspace()
from <ctype.h>
to check each character in the string. The function isspace()
returns a non-zero value if the character is a whitespace (e.g., space, tab, newline).
main.c
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
bool isWhitespaceOnly(const char *str) {
while (*str) {
if (!isspace((unsigned char)*str))
return false;
str++;
}
return true;
}
int main() {
const char *test1 = " \t\n"; // Contains only whitespace: spaces, tab, newline
const char *test2 = " abc "; // Contains non-whitespace characters
printf("Test1: %s\n", isWhitespaceOnly(test1) ? "Only whitespace" : "Contains non-whitespace");
printf("Test2: %s\n", isWhitespaceOnly(test2) ? "Only whitespace" : "Contains non-whitespace");
return 0;
}
Explanation:
- The function
isWhitespaceOnly
accepts a constant character pointerstr
as its input. - A
while
loop iterates through each character of the string until the null terminator (\0
) is reached. - The
isspace()
function checks whether the current character is a whitespace character (space, tab, newline, etc.). - If a non-whitespace character is found, the function immediately returns
false
. - If the loop completes without finding any non-whitespace characters, the function returns
true
. - The
main
function demonstrates the usage ofisWhitespaceOnly
with two test strings and prints the corresponding output.
Output:
Test1: Only whitespace
Test2: Contains non-whitespace
2. Using Manual Character Comparison
In this example, we manually check if each character in the string is a whitespace by comparing it against common whitespace characters such as space, tab, newline, vertical tab, form feed, and carriage return.
main.c
#include <stdio.h>
#include <stdbool.h>
bool isWhitespaceOnly(const char *str) {
while (*str) {
char ch = *str;
if (ch != ' ' && ch != '\t' && ch != '\n' && ch != '\v' && ch != '\f' && ch != '\r') {
return false;
}
str++;
}
return true;
}
int main() {
const char *test1 = " \t\n"; // Contains only whitespace
const char *test2 = " hello "; // Contains non-whitespace characters
printf("Test1: %s\n", isWhitespaceOnly(test1) ? "Only whitespace" : "Contains non-whitespace");
printf("Test2: %s\n", isWhitespaceOnly(test2) ? "Only whitespace" : "Contains non-whitespace");
return 0;
}
Explanation:
- The function
isWhitespaceOnly
is defined to manually verify each character of the input string. - A
while
loop traverses the string until the end (null terminator). - Each character is assigned to the variable
ch
and is compared against standard whitespace characters: space, tab, newline, vertical tab, form feed, and carriage return. - If any character does not match these whitespace characters, the function returns
false
. - If every character in the string is a whitespace, the function returns
true
after the loop completes. - The
main
function tests this approach using two different strings and prints the results accordingly.
Output:
Test1: Only whitespace
Test2: Contains non-whitespace
Conclusion
In this tutorial, we learned two different methods to check if a string contains only whitespace characters in C. The first example used the isspace()
function from <ctype.h>
, and the second example demonstrated manual character comparisons.