📘 Lesson  ·  Lesson 67

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.

💡 This is the key idea

Almost every hand-written string function is just "walk through the characters until you hit '\0'."

Length without strlen

C Language
#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;
}
Output:
Length = 12

The loop counts characters until it meets '\0' — that count is the length.

Copy without strcpy

C Language
#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;
}
Output:
Copied: Hello
⚠️ Don't forget the '\0'

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.

C Language
#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;
}
Output:
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

C Language
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 <= j and needlessly swapping the middle character with itself.
🏋️ Practice

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?
You start a counter at zero and walk through the string one character at a time until you reach the null terminator '\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?
Every C string ends with a hidden special character '\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?
First find the length, then swap characters from both ends moving inward: swap the first with the last, the second with the second-last, and so on until the two indices meet in the middle. This reverses the string in place without any library function.
How do you copy a string without strcpy?
You loop through the source string character by character and assign each one to the same position in the destination, stopping at the null terminator — and then you must copy the null terminator too, so the destination is a proper string.
Why learn to write string functions from scratch?
Writing them yourself teaches how strings, loops, arrays and the null terminator really work, which is a common interview topic and builds a solid mental model. In real projects you would normally use the tested string.h functions instead.
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.