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

</>
Copy
#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:

  1. The function isWhitespaceOnly accepts a constant character pointer str as its input.
  2. A while loop iterates through each character of the string until the null terminator (\0) is reached.
  3. The isspace() function checks whether the current character is a whitespace character (space, tab, newline, etc.).
  4. If a non-whitespace character is found, the function immediately returns false.
  5. If the loop completes without finding any non-whitespace characters, the function returns true.
  6. The main function demonstrates the usage of isWhitespaceOnly 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

</>
Copy
#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:

  1. The function isWhitespaceOnly is defined to manually verify each character of the input string.
  2. A while loop traverses the string until the end (null terminator).
  3. 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.
  4. If any character does not match these whitespace characters, the function returns false.
  5. If every character in the string is a whitespace, the function returns true after the loop completes.
  6. 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.