Bird
0
0
DSA Cprogramming

String Basics and Memory Representation in DSA C

Choose your learning style9 modes available
Mental Model
A string is a sequence of characters stored one after another in memory, ending with a special marker to show where it stops.
Analogy: Think of a string like a train of connected boxes, each holding one letter, with a special box at the end that says 'this is the last one'.
S -> [H] -> [E] -> [L] -> [L] -> [O] -> [\0]
↑start pointer
Dry Run Walkthrough
Input: string: "HELLO" stored in memory
Goal: Understand how each character is stored in order and how the string ends
Step 1: Store 'H' at first memory location
S -> [H] -> [] -> [] -> [] -> [] -> []
↑start pointer
Why: First character must be stored at the start
Step 2: Store 'E' at next memory location
S -> [H] -> [E] -> [] -> [] -> [] -> []
↑start pointer
Why: Characters are stored one after another
Step 3: Store 'L' at next memory location
S -> [H] -> [E] -> [L] -> [] -> [] -> []
↑start pointer
Why: Continue storing characters in order
Step 4: Store second 'L' at next memory location
S -> [H] -> [E] -> [L] -> [L] -> [] -> []
↑start pointer
Why: Store repeated characters as normal
Step 5: Store 'O' at next memory location
S -> [H] -> [E] -> [L] -> [L] -> [O] -> []
↑start pointer
Why: Store last character before end marker
Step 6: Store '\0' (null character) to mark end
S -> [H] -> [E] -> [L] -> [L] -> [O] -> [\0]
↑start pointer
Why: Null character shows where string ends in memory
Result:
S -> [H] -> [E] -> [L] -> [L] -> [O] -> [\0]
String ends with null character to mark termination
Annotated Code
DSA C
#include <stdio.h>

int main() {
    char str[] = "HELLO"; // string stored with null terminator

    // Print each character and its memory index
    for (int i = 0; str[i] != '\0'; i++) {
        printf("Index %d: %c\n", i, str[i]);
    }

    // Print the null terminator explicitly
    printf("Index %d: %d (null terminator)\n", 5, str[5]);

    return 0;
}
for (int i = 0; str[i] != '\0'; i++) {
Traverse characters until null terminator to print each character
printf("Index %d: %c\n", i, str[i]);
Print character at current index
printf("Index %d: %d (null terminator)\n", 5, str[5]);
Print the null terminator value explicitly to show string end
OutputSuccess
Index 0: H Index 1: E Index 2: L Index 3: L Index 4: O Index 5: 0 (null terminator)
Complexity Analysis
Time: O(n) because we visit each character once to print
Space: O(n) because the string uses memory proportional to its length plus one for the null terminator
vs Alternative: Compared to storing characters without a null terminator, this method allows easy detection of string end but requires extra space for the terminator
Edge Cases
Empty string ""
Only the null terminator is stored at the first position, indicating no characters
DSA C
for (int i = 0; str[i] != '\0'; i++) {
String with one character "A"
Character 'A' stored at index 0, null terminator at index 1
DSA C
for (int i = 0; str[i] != '\0'; i++) {
When to Use This Pattern
When you see problems about storing or manipulating text in low-level languages, think about strings as arrays of characters ending with a null marker to know where they stop.
Common Mistakes
Mistake: Forgetting the null terminator at the end of the string
Fix: Always ensure strings end with '\0' so functions know where the string ends
Summary
A string is a sequence of characters stored in memory ending with a null character to mark its end.
Use this when you need to store and process text in languages like C that do not store string length separately.
The key insight is the null terminator '\0' which tells the program where the string stops.