Matt Matt - 1 year ago 91
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.


Answer Source

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;
  return 1;

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download