Strings
Text is just an array of characters. Learn how computers store words, how to manipulate them in C, and solve the classic string problems that appear in every interview.
Quick answer
In C, a string is an array of characters ending with a null terminator '\0'. The <string.h> functions strlen, strcpy, strcat and strcmp work on that convention; compare strings with strcmp, never ==.
- Allocate length + 1 bytes for the terminating '\0'.
- == compares addresses, not contents.
- Many interview problems are string scans with two indices.
#include <stdio.h>
#include <string.h>
int main(void) {
char word[] = "stack";
printf("length = %zu\n", strlen(word));
printf("same? %s\n", strcmp(word, "stack") == 0 ? "yes" : "no");
for (size_t i = 0, j = strlen(word) - 1; i < j; i++, j--) {
char t = word[i]; word[i] = word[j]; word[j] = t;
}
printf("reversed = %s\n", word);
return 0;
}Output
length = 5 same? yes reversed = kcats
Every piece of text your program ever handles — a name, a password, an email address, a sentence — is a string. And a string is nothing more than an array of characters sitting in memory, one after another, with a special signal at the end that says "stop here."
Because you already understand arrays from Lesson 3, strings will feel natural. We just need to learn a few new rules — especially that special signal at the end — and then practise the classic problems that show up in almost every coding interview.
What is a String?
In C, a string is an array of char values — where each character is stored as a number according to the ASCII table. The letter 'A' is stored as 65. 'a' is 97. '0' is 48. Your computer never stores letters — only numbers.
The Null Terminator — the most important rule in C strings
Every string in C ends with a special character called the null terminator, written as '\0'. Its ASCII value is 0. It is not a space — it is a completely invisible marker that tells every string function "the string ends here, stop reading."
Strings in C — Declaration and Input
Let us see how to create, read, and print strings in C — with every detail explained.
Reading a string from the user
scanf("%s") stops reading at a space. So "Asil Khan" would only store "Asil". To read a full line including spaces, use fgets(name, 50, stdin) instead. We will use this in the examples below.Built-in String Functions in C
C gives you a library called string.h with ready-made functions for common string operations. You include it the same way as stdio.h. Knowing these saves you from writing everything from scratch.
| Function | What it does | Example | Returns |
|---|---|---|---|
| strlen(s) | Count characters (not including \0) | strlen("HELLO") | 5 |
| strcpy(dest, src) | Copy src into dest | strcpy(a, "Hi") | void |
| strcat(dest, src) | Append src to end of dest | strcat(a, " World") | void |
| strcmp(s1, s2) | Compare two strings | strcmp("abc", "abc") | 0 if equal |
| strchr(s, c) | Find first occurrence of char c | strchr("hello", 'l') | pointer or NULL |
| strstr(s1, s2) | Find s2 inside s1 | strstr("hello world", "world") | pointer or NULL |
| strupr(s) | Convert to uppercase | strupr("hello") | "HELLO" |
| strlwr(s) | Convert to lowercase | strlwr("HELLO") | "hello" |
Traversing a String — Character by Character
Since a string is an array, you can loop through it just like any array. The trick is knowing when to stop — and the answer is the null terminator. When you hit '\0', you stop.
Classic String Problems — With Full Solutions
These are the exact problems that appear in every beginner and intermediate coding interview. Understand the logic — not just the code.
Same two-pointer technique as reversing an array from Lesson 3. One pointer at the start, one at the end — swap and move inward until they meet.
A palindrome reads the same forwards and backwards — "madam", "racecar", "level". Compare characters from both ends — if any pair doesn't match, it's not a palindrome.
Walk through each character. If it is a, e, i, o, u (uppercase or lowercase) it is a vowel. If it is any other letter it is a consonant. Spaces and digits are neither.
Two strings are anagrams if they contain the exact same characters in any order. "listen" and "silent" are anagrams. The approach: count how many times each letter appears in string 1, then subtract for string 2. If all counts reach zero — anagram.
A new word starts when you see a non-space character after a space (or at the very beginning). Track whether the previous character was a space — that is all you need.
Walk through the string once. Every time you find the target character, replace it. Single pass — O(n).
Real Mistakes — And Exactly How to Fix Them
These are the three string mistakes that trip up almost every C beginner. Read them now so you do not waste hours debugging them later.
Quick Reference — Operations Summary
| Operation | Method | Time | Key point |
|---|---|---|---|
| Get length | strlen(s) | O(n) | Walks to \0 — not O(1) |
| Copy string | strcpy(dest, src) | O(n) | dest must be large enough |
| Concatenate | strcat(dest, src) | O(n) | dest must have room for both |
| Compare | strcmp(s1, s2) | O(n) | Returns 0 if equal |
| Reverse | Two pointer swap | O(n) | Works in-place, O(1) space |
| Palindrome check | Two pointer compare | O(n) | Stop at first mismatch |
| Anagram check | Frequency array | O(n) | 26-slot int array for a–z |
| Traverse | Loop until \0 | O(n) | Use != '\0' as stop condition |
You Are Ready for Lesson 5
You now understand strings fully — how they sit in memory, how to manipulate them, and how to solve the classic problems. You have also seen the frequency array trick which appears constantly in string, array, and hashing problems.
In Lesson 5 we tackle Pointers — the concept that confuses most beginners but is the backbone of everything advanced in C. You have already seen pointers briefly with the & in scanf and with array names being addresses. Now we make it all explicit and clear.
🎯 Key Takeaways
- ✓A string in C is a char array that ends with a null terminator \0 — without it, the string has no end
- ✓Always declare char arrays with size n+1 — the extra slot is for \0
- ✓Use %s to print strings, strlen() to get length, strcpy() to copy, strcat() to join, strcmp() to compare
- ✓Never use == to compare strings — use strcmp(s1, s2) == 0
- ✓Traverse a string with a loop that stops when it hits \0, not when it hits a fixed size
- ✓The frequency array trick (int freq[26]) is the key to solving anagram, duplicate, and counting problems
- ✓The two-pointer technique from arrays works perfectly for reversing strings and palindrome checks
- ✓Lowercase letter to array index: use char - 'a' (e.g. 'c' - 'a' = 2)
Quick check
How many bytes does the string "hello" need in a char array?
HELP US IMPROVE
How was this lesson?
Your note is shared with the Chaduvuko team for review. Please don’t include personal or sensitive information.
Spotted a mistake or something unclear? Suggest a fix on GitHub or email us. Select the text first and it is quoted for you.
Discussion
0Have a better approach? Found something outdated? Share it — your knowledge helps everyone learning here.
