Back to Blog

C in 100 Seconds: String Functions — strcat, strchr, strstr

Daryl WongDaryl Wong

Video: C in 100 Seconds: String Functions — strcat, strchr, strstr | Episode 32 by Taught by Celeste AI - AI Coding Coach

Watch full page →

String Functions in C

C doesn't have a string type — strings are just arrays of characters ending with a null byte. The string.h header provides functions to work with them.

strcat — Append Strings

char greeting[50] = "Hello";
strcat(greeting, " World");
// greeting is now "Hello World"

The destination buffer must be large enough to hold both strings plus the null terminator. If it's too small, you get a buffer overflow.

strncat — Safer Append

char buf[20] = "Hi";
strncat(buf, " there, friend!", 6);
// buf is now "Hi there" (only 6 chars appended)

strncat limits how many characters get appended, helping prevent overflows.

strchr — Find a Character

char path[] = "/home/user/docs/file.txt";
char *dot = strchr(path, '.');
// dot points to ".txt"

Returns a pointer to the first occurrence of the character, or NULL if not found.

strstr — Find a Substring

char sentence[] = "The quick brown fox";
char *found = strstr(sentence, "brown");
// found points to "brown fox"

Returns a pointer to where the substring starts, or NULL if not found.

Student Code

Try it yourself: episode32/strings.c