Search code examples
ccstring

What techniques are available to test if a character string is all spaces?


char *p = "   woohoo";

int condition = /* some calculation applied to p            */ 
                /* to look for all 0x20/blanks/spaces only  */ 

if (condition)
{
}
else
{
    printf("not ");
}

printf("all spaces\n");

Solution

  • One-liner:

    int condition = strspn(p, " ") == strlen(p);
    

    Slightly more optimized:

    int condition = p[strspn(p, " ")] == '\0';