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
strcpyandstrcatbuild a string intodest.strlenmeasures it;strcmpcompares.strchrfinds the first matching character.
Keywords and builtins used here
NULLcharconstgreetintreturnsize_t
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.
Step 2 of 2 in Strings in memory, step 23 of 25 in Pointers & memory.