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
}

Recommended Answers

All 2 Replies

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

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
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.