Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 895
Matt Avatar asked Oct 20 '10 19:10

Matt


1 Answers

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.

like image 75
casablanca Avatar answered Oct 07 '22 22:10

casablanca