How to check if a string is empty or have only whitespaces without characters in C

2 Answers

0 votes
#include <stdio.h> 
#include <ctype.h> 

int is_empty(const char *s) ;
 
int main(void)
{   
    char s[15] = "";
    
    if (is_empty(s))
        printf("empty\n");
    else    
        printf("not empty\n");
               
    return 0;
}
int is_empty(const char *s) 
{
    while (*s != '\0') 
    {
        if (!isspace(*s))
          return 0;
        s++;
    }
    return 1;
}


   
/*
run:

empty

*/

 



answered Nov 18, 2016 by avibootz
0 votes
#include <stdio.h> 
#include <ctype.h> 

int is_empty(const char *s) ;
 
int main(void)
{   
    char s[15] = " \n\r\t";
    
    if (is_empty(s))
        printf("empty\n");
    else    
        printf("not empty\n");
               
    return 0;
}
int is_empty(const char *s) 
{
    while (*s != '\0') 
    {
        if (!isspace(*s))
          return 0;
        s++;
    }
    return 1;
}


   
/*
run:

empty

*/

 



answered Nov 18, 2016 by avibootz
...