Study smarter with Fiveable
Get study guides, practice questions, and cheatsheets for all your subjects. Join 500,000+ students with a 96% pass rate.
String manipulation is fundamental to nearly every C program you'll write—from parsing user input to processing file data to building command-line tools. These functions from <string.h> represent the core toolkit you're expected to master, and exam questions frequently test not just what each function does, but why you'd choose one over another. You're being tested on your understanding of memory safety, buffer management, and pointer arithmetic—concepts that separate competent C programmers from those who write vulnerable code.
Don't just memorize function signatures. Know which function to use when, understand the security implications of unbounded operations versus bounded alternatives, and recognize how these functions interact with C's null-terminated string model. When you see a question about string handling, ask yourself: What could go wrong here? What's the safer alternative?
Before you can manipulate a string, you need to understand its contents. These functions let you measure length and locate specific characters or patterns—all by traversing the string until they hit the null terminator.
\0const char *str) and is essential for calculating buffer sizes before copying or concatenatingCompare: strchr() vs. strstr()—both search and return pointers (or NULL), but strchr() finds a single character while strstr() finds a multi-character pattern. If an exam asks about parsing a file path, strchr() finds the / delimiter; strstr() finds a filename pattern.
Copying strings requires careful attention to buffer sizes. The destination must always have enough allocated memory to hold the source string plus its null terminator—getting this wrong causes buffer overflows.
Compare: strcpy() vs. strncpy()—strcpy() is simpler but dangerous; strncpy() is safer but requires manual null termination. Exam tip: if asked about secure string handling, always mention that strncpy() needs dest[n-1] = '\0' to guarantee termination.
Concatenation appends one string to another. The destination must have enough space for both strings plus the null terminator—a common source of bugs.
strncpy(), this function guarantees proper terminationCompare: strcat() vs. strncat()—both concatenate and null-terminate, but strncat() limits how many characters are appended. Key difference from the copy functions: strncat() always adds a null terminator, while strncpy() might not.
You cannot compare C strings with == (that compares pointer addresses). These functions perform lexicographic comparison based on ASCII values, character by character.
'A' (ASCII 65) differs from 'a' (ASCII 97)strcmp()—zero for equal, negative/positive for less/greaterCompare: strcmp() vs. strncmp()—use strcmp() for full equality checks; use strncmp() when you only care about the beginning of strings (like checking command prefixes). Both are but strncmp() can terminate earlier.
Tokenization breaks a string into smaller pieces based on delimiters—essential for parsing structured text like CSV files or command-line input.
Compare: strtok() vs. manual parsing with strchr()—strtok() is convenient but destructive and not reentrant. For multi-threaded code or when you need to preserve the original string, consider strtok_r() (POSIX) or manual parsing.
| Concept | Best Examples |
|---|---|
| Measuring length | strlen() |
| Character search | strchr() |
| Substring search | strstr() |
| Unbounded copy | strcpy() |
| Bounded copy | strncpy() |
| Unbounded concatenation | strcat() |
| Bounded concatenation | strncat() |
| Full comparison | strcmp() |
| Partial comparison | strncmp() |
| Tokenization | strtok() |
| Security-conscious operations | strncpy(), strncat(), strncmp() |
Which two functions share the property of not guaranteeing null termination, and why does this matter for security?
You need to check if a filename ends with .txt. Which function would you use, and what would you compare against the return value?
Compare and contrast strcpy() and strncpy(): when would you choose each, and what extra step does strncpy() require?
If you're parsing a comma-separated string like "apple,banana,cherry", which function would you use? What happens to the original string?
A function receives user input and needs to copy it into a fixed-size buffer of 64 bytes. Write the two lines of code that safely copy and guarantee null termination.