Stack for string reverse

To reverse a string, you need the last character to become first, the second-to-last to become second, and so on. A stack’s LIFO rule does exactly that: the last character you push is the first you can pop. So one pass pushes every character in normal reading order; a second pass pops them all into a new buffer—yielding the reversed string. Unlike browser history, this pattern uses one stack (no forward branch).

On this page
  • Why LIFO reverses — push order vs pop order
  • Rules — load the stack, drain it into output
  • Example in Chelloolleh
  • Trace — stack and output after each step

1) One stack, reversed order

How the stack lines up with the string
Phase What happens
Push pass Scan the input left to right. Each character is pushed. The bottom of the stack holds the first input character; the top holds the last.
Pop pass While the stack is not empty, pop and append to the output. The first pop is the last input character, so the output grows in reverse order.

2) Rules

Operations for the tiny string reverser
Event Effect
Read input left → right For each character c: push c onto the stack (if there is room).
Drain stack Repeatedly pop and write the character to the next slot of the output buffer until the stack is empty. NUL-terminate the output.

Snapshot for input "hello" after the push phase:

Goal output: olleh

Stack (after pushes)

Bottom → top (reading hello)

h e l l o

The first pop removes o—the last character you pushed.

Output buffer

While popping (first pops shown)

o … then l

Each pop appends to the right, building olleh when the stack is empty.

In production you would check overflow before every push and size buffers safely; Unicode needs wide-character handling, not raw char alone.

3) Example in C

The helper reverse_string copies the reversed bytes of in into out, which must be at least as large as in. The demo reverses the literal "hello" and prints olleh.

#include <stdio.h>

#define MAX 128

char stack[MAX];
int top = -1;

static void push(char c) {
    stack[++top] = c;
}

static char pop(void) {
    if (top < 0)
        return '\0';
    return stack[top--];
}

// Push all, then pop all into out
static void reverse_string(const char *in, char *out) {
    int i, j;

    top = -1;
    for (i = 0; in[i] != '\0'; i++)
        push(in[i]);

    j = 0;
    while (top >= 0)
        out[j++] = pop();
    out[j] = '\0';
}

int main(void) {
    char out[MAX];

    reverse_string("hello", out);
    printf("%s\n", out);
    return 0;
}

Expected line: olleh

Trace (abbreviated)

Stack shows characters bottom → top. Output grows as pops occur.

Reversing hello
Step Action Stack (bottom → top) Output so far
1–5 Push ho h e l l o (empty)
6 Pop → append h e l l o
7 Pop → append h e l ol
8–10 Pop remaining (empty) olleh

4) Trade-offs

  • Pros — Teaches LIFO clearly; O(n) time and O(n) extra space for the stack buffer.
  • Cons — Two-pointer in-place reverse uses O(1) extra space; stacks shine when you already need LIFO (e.g. parsing). The same idea supports palindrome checks by comparing half the string with pops.

Related: Stack for browser history · Stack applications · Stack introduction.

Key takeaway

Push preserves left-to-right order inside the stack; pop reads back right-to-left. That swap of order is exactly string reversal using one stack.

See also: Stack for browser history · Stack tutorial