String Without string.h
Why write string functions by hand?
C gives you ready-made string tools in <string.h> — strlen, strcpy, strrev and more. But writing your own versions is one of the best ways to truly understand strings, and it is a very common interview exercise.
Everything below uses only loops and arrays — no string.h. Once you see how these work, the library functions will never feel like magic again.
The secret: the null terminator
Every C string secretly ends with a special character, '\0' (the null terminator). The word "Hi" is really stored as three characters: 'H', 'i', '\0'. That final '\0' is how every function knows where the string ends.
Almost every hand-written string function is just "walk through the characters until you hit '\0'."
Length without strlen
#include <stdio.h>
int main() {
char str[] = "CodingEasily";
int len = 0;
while (str[len] != '\0') // stop at the terminator
len++;
printf("Length = %d\n", len);
return 0;
}Length = 12
The loop counts characters until it meets '\0' — that count is the length.
Copy without strcpy
#include <stdio.h>
int main() {
char src[] = "Hello";
char dest[20];
int i = 0;
while (src[i] != '\0') { dest[i] = src[i]; i++; }
dest[i] = '\0'; // copy the terminator too!
printf("Copied: %s\n", dest);
return 0;
}Copied: Hello
After the loop you must add dest[i] = '\0'; yourself, or dest is not a valid string and printing it prints garbage.
Reverse a string
Find the length, then swap the ends inward until they meet.
#include <stdio.h>
int main() {
char str[] = "coding";
int len = 0;
while (str[len] != '\0') len++;
for (int i = 0, j = len - 1; i < j; i++, j--) {
char t = str[i]; str[i] = str[j]; str[j] = t; // swap
}
printf("Reversed: %s\n", str);
return 0;
}Reversed: gnidoc
The two indices i and j move toward each other, swapping as they go. When they meet, the string is fully reversed.
Compare two strings
int i = 0;
while (a[i] != '\0' && b[i] != '\0') {
if (a[i] != b[i]) break; // first difference
i++;
}
if (a[i] == b[i]) printf("Equal\n");
else printf("Not equal\n");Walk both strings together; the moment two characters differ, they are not equal. If you reach the end of both at once, they match.
Common mistakes
- Forgetting to add
'\0'after copying, leaving an invalid string. - Making the destination array too small to hold the copy plus
'\0'. - Looping with
<=length and reading one character too far. - In reverse, using
i <= jand needlessly swapping the middle character with itself.
Write your own concat that joins two strings: copy the first, then continue copying the second right after it, and finish with '\0'. Test with "Coding" and "Easily".
Summary
- Every C string ends with the null terminator
'\0'. - Length: count characters until
'\0'. - Copy: assign each character, then add
'\0'yourself. - Reverse: swap characters from both ends inward.
- Compare: walk both strings until they differ or both end.
Frequently Asked Questions
How do you find the length of a string without strlen in C?
'\0', increasing the counter each step. When the loop stops, the counter equals the length. This is exactly what strlen does internally.What is the null terminator in a C string?
'\0', called the null terminator. It marks where the string stops. All string functions — whether built-in or hand-written — rely on it to know where the text ends.How do you reverse a string without strrev in C?
How do you copy a string without strcpy?
Why learn to write string functions from scratch?
string.h functions instead.