Library contents

C strspn

Determine whether a field supplies any content after ignoring ordinary whitespace.

A simple example

strspn

  1. Start with " "
  2. Do this Check for blank text
  3. Result Yes
Removing the spaces leaves no visible content.
lesson.c
#include <stdio.h>
#include <string.h>

int main(void) {
    const char *value = "   ";
    printf("%s\n", (strspn(value, " \t\r\n") == strlen(value)) ? "yes" : "no");
    return 0;
}
Output
yes

Syntax

C syntax
strspn(value, " \t\r\n") == strlen(value)
Run these examples in C

Save one example at a time as lesson.c. Use C17 and a Unix-style shell.

cc -std=c17 -Wall -Wextra lesson.c -lm -o lesson
./lesson

Check an attendance comment

A form accepts an optional note typed or pasted by a customer or student. Decide whether the field contains meaningful text before displaying it to the person who will act on the request.

From the programconst char *value = " \t "
Resultyes
lesson.c
#include <stdio.h>
#include <string.h>

int main(void) {
    const char *value = " \t ";
    printf("%s\n", (strspn(value, " \t\r\n") == strlen(value)) ? "yes" : "no");
    return 0;
}
Output
yes

Spaces and a tab supply no comment.

Advertisements

Check an aisle request

A traveler types a seating preference with padding around the word aisle. Check whether the preference still contains useful text after the surrounding whitespace is removed.

From the programconst char *value = " aisle "
Resultno
lesson.c
#include <stdio.h>
#include <string.h>

int main(void) {
    const char *value = " aisle ";
    printf("%s\n", (strspn(value, " \t\r\n") == strlen(value)) ? "yes" : "no");
    return 0;
}
Output
no

The word aisle is meaningful after trimming.

Review a space-only answer

A student pressed the space bar three times before submitting a response. The submission form treats whitespace-only text as a blank response. Which status should the reviewer see?

GivenThree ASCII spaces
Worked resultBlank

What to watch for

Blankness is a field policy. Choose which Unicode whitespace characters the application should ignore.

Documentation for strspn