getline check if line is whitespace

Matt picture Matt · Oct 20, 2010 · Viewed 23.6k times · Source

Is there an easy way to check if a line is empty. So i want to check if it contains any white space such as \r\n\t and spaces.

Thanks

Answer

casablanca picture casablanca · Oct 20, 2010

You can use the isspace function in a loop to check if all characters are whitespace:

int is_empty(const char *s) {
  while (*s != '\0') {
    if (!isspace((unsigned char)*s))
      return 0;
    s++;
  }
  return 1;
}

This function will return 0 if any character is not whitespace (i.e. line is not empty), or 1 otherwise.