typestar

String library functions in C

The <string.h> workhorses for C strings.

#include <string.h>

int greet(char *dest, const char *name) {
    strcpy(dest, "Hello, ");
    strcat(dest, name);

    size_t len = strlen(dest);
    int same = strcmp(dest, "Hello, world") == 0;
    char *comma = strchr(dest, ',');
    return (int) len + same + (comma != NULL);
}

How it works

  1. strcpy and strcat build a string into dest.
  2. strlen measures it; strcmp compares.
  3. strchr finds the first matching character.

Keywords and builtins used here

The run, in numbers

Lines
11
Characters to type
259
Tokens
84
Three-star pace
100 tpm

At the three-star pace of 100 tokens a minute, this run takes about 50 seconds.

Type this snippet

Step 2 of 2 in Strings in memory, step 23 of 25 in Pointers & memory.

← Previous Next →