Library contents

C strlen length check

Measure the byte length of the terminated parcel label before printing its depot prefix.

A simple example

strlen length check

  1. Start with Text "A"
  2. Do this Request a two-character prefix
  3. Result Too short
Only one character is available. Check the length before extracting two.

Syntax

C syntax
strlen(code) < prefix_length
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

Read the depot prefix on a damaged label

A parcel scanner expects a three-character depot prefix. A torn label supplies only two characters, so the parcel should go to manual sorting before the software extracts the prefix.

From the programconst char *code = "NE"
ResultSend to manual sorting
lesson.c
#include <stdio.h>
#include <string.h>

int main(void) {
    const char *code = "NE";
    if (strlen(code) < 3) {
        puts("Send to manual sorting");
    } else {
        printf("%.3s\n", code);
    }
    return 0;
}
Output
Send to manual sorting

Two letters cannot identify the three-letter depot. The code routes the parcel to a clerk.

Advertisements

Read a complete lab identifier

A student scans a box showing the code BIO7. Extract the three-character prefix. A short scan must be repeated before the record is filed.

GivenBIO7 · take 3
Worked resultPrefix available

Identify an incomplete scan

A label is folded under the scanner and only two letters reach the form. Extract the three-character prefix. A short scan must be repeated before the record is filed.

GivenAR · take 3
Worked resultRescan

What to watch for

strlen requires a null-terminated string and counts bytes. The depot codes in this example use ASCII.

Documentation for strlen length check