Matt Matt - 3 months ago 13
C Question

getline check if line is whitespace

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

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(*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.