chris360 chris360 - 1 month ago 4x
C Question

How to check if user did not enter all whitespace characters in C

When prompting the user to enter there name, it is necessary that the user indeed entered a name and not just whitespace.

Output ex:

Your name is: /user entered only whitespace/

How would one validate that characters were actually implemented, and not just spaces? Im using fgets() to retreive my input however i am unable to post the code now for I am at work and doing this off my mobile device. If code is nessesary i will post what im working on late tonight (pacific standard time). Thank you!


One way to test is to check all characters if they are space.

#include <stdio.h>
#include <stdlib.h>
// TODO: you should use bool instead
int is_all_space(const char *s)
  char *tmp;
  tmp = (char *) s;
  // you might check for '\n' instead in your case
  // or even '\r'
  while (*tmp != '\0') {
    // TODO: this check will fail if the input has zero length
    if (*tmp != ' ') {
      return 0;
  return 1;
// don't forget to put the argument in quotes if it contains spaces
int main(int argc, char **argv)
  if (argc < 2) {
    fprintf(stderr, "Usage: %s somestring\n", argv[0]);

  if (is_all_space(argv[1])) {
    puts("Input is all-space and nothing else");
  } else {
    puts("Input it not all-space");