for some reason it always return 1; lets say char line will have a empty line or not empty line.

int main(void)
{
char line[20];
//user may enter some word or enter nothing in LINE

 if(empty_line(line) != 0)
 {
   //...
 }
}


int empty_line(char line[])
{
  for(i = 0; i < 20; i++)
      {
          if(line[i] == ' ' || line[i] =='\t' || line[i] == '\n')
             {
             }
            else
            {
             return 1;   //not empty
            }
       }
       return 0;  //empty
}

Edited 4 Years Ago by hwoarang69

int main(
    )
    {
     while(fgets(line, L_SIZE, finp1) != NULL)
         {
             if(empty_line(line) != 0)                   //ALWAYS GOES INTO THIS IF STATMENTS :(
             {
             //do some thing
             }
         }
    }

Edited 3 Years Ago by ~s.o.s~: formatting

Your first error is assuming there are exactly 20 characters in the line. What if there are 25 spaces? Or only 1 character and it is a space? To solve that problem check for end-of-line terminating character '\0'.

int empty_line(char line[])
{
   int i;
   for(i = 0; line[i] != '\0'; i++)
   {
      if( !isspace(line[i]) && line[i] != '\n')
         return 0; // not an empty line
   }
   return 1; // empty line
}

Edited 4 Years Ago by Ancient Dragon

This article has been dead for over six months. Start a new discussion instead.