C String strrev() Function

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
Extension / loop

What You’ll Learn

The strrev() function reverses a C-string in place by swapping characters from both ends toward the center. It is not ISO C standard—many compilers offer it as an extension. This tutorial also teaches the portable two-pointer algorithm every platform supports.

01

In place

Same buffer.

02

Swap ends

Two pointers.

03

Returns str

Same pointer.

04

Not ISO C

Extension.

05

Copy first

Keep original.

06

Palindrome

On a copy.

Definition and Usage

strrev stands for string reverse. Given "Hello, C!", the result is "!C ,olleH". Spaces and punctuation move with their positions—only the order of bytes changes. The null terminator stays at the end.

Because the original buffer is overwritten, copy the string first if you still need the unreversed text. Never pass a string literal; reversing requires writable memory.

💡
Beginner Tip

After strrev(buf), both “before” and “after” refer to the same memory—now reversed. Print the original text before calling strrev, or strcpy into a second buffer first.

📝 Syntax

Typical extension declaration:

C
char *strrev(char *str);

Portable alternative

C
#include <string.h>

void reverse_string(char *s) {
    size_t left = 0;
    size_t right = strlen(s);

    if (right == 0) {
        return;
    }
    right--;

    while (left < right) {
        char tmp = s[left];
        s[left] = s[right];
        s[right] = tmp;
        left++;
        right--;
    }
}

Parameter

  • str — writable null-terminated string to reverse.

Return Value

  • Returns str (pointer to the reversed buffer).

⚡ Quick Reference

BeforeAfter reverse
"Hello, C!""!C ,olleH"
"abc""cba"
"ab""ba"
"a""a" (unchanged)
"""" (empty)
Extension
strrev(buf);

If available

Portable
reverse_string(buf);

Swap loop

Keep original
strcpy(copy, src);

Then reverse copy

Palindrome
compare both ends

No modify

Examples Gallery

Examples use the portable reverse_string helper so they compile on Linux, macOS, and Windows. Behavior matches typical strrev extensions.

📚 Getting Started

Reverse a writable char array and print the result.

Example 1 — Reverse "Hello, C!"

Print the original, then reverse in place and show the new order.

C
#include <stdio.h>
#include <string.h>

void reverse_string(char *s) {
    size_t left = 0, right = strlen(s);
    if (right == 0) return;
    right--;
    while (left < right) {
        char tmp = s[left];
        s[left++] = s[right];
        s[right--] = tmp;
    }
}

int main(void) {
    char text[] = "Hello, C!";

    printf("Before: %s\n", text);
    reverse_string(text);
    printf("After:  %s\n", text);

    return 0;
}

How It Works

Printing before the reverse call preserves the original wording for comparison. After swapping, the same buffer holds the reversed bytes.

Example 2 — Two-Pointer Swap (Step by Step)

See how start and end move toward the center.

C
#include <stdio.h>
#include <string.h>

void reverse_string(char *str) {
    int start = 0;
    int end = (int)strlen(str) - 1;

    while (start < end) {
        char temp = str[start];
        str[start] = str[end];
        str[end] = temp;
        start++;
        end--;
    }
}

int main(void) {
    char word[] = "CodeToFun";

    reverse_string(word);

    printf("Reversed: %s\n", word);

    return 0;
}

How It Works

Each iteration swaps the outer pair, then moves inward. When start >= end, every character has been placed correctly. Middle character in odd-length strings never moves.

📈 Practical Patterns

Preserve originals, test palindromes, and handle edge lengths.

Example 3 — Reverse a Copy, Keep the Original

Use strcpy when both versions must appear in output.

C
#include <stdio.h>
#include <string.h>

void reverse_string(char *s) {
    size_t l = 0, r = strlen(s);
    if (r == 0) return;
    r--;
    while (l < r) {
        char t = s[l];
        s[l++] = s[r];
        s[r--] = t;
    }
}

int main(void) {
    const char *original = "Hello, C!";
    char buffer[32];

    strcpy(buffer, original);
    reverse_string(buffer);

    printf("Original: %s\n", original);
    printf("Reversed: %s\n", buffer);

    return 0;
}

How It Works

This fixes a common beginner mistake: calling strrev on the only copy and then trying to print “original” and “reversed” from the same buffer.

Example 4 — Simple Palindrome Check (No Modify)

Compare characters from both ends without reversing the string.

C
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int is_palindrome(const char *s) {
    size_t left = 0;
    size_t right = strlen(s);

    if (right == 0) return 1;
    right--;

    while (left < right) {
        if (tolower((unsigned char)s[left]) !=
            tolower((unsigned char)s[right])) {
            return 0;
        }
        left++;
        right--;
    }
    return 1;
}

int main(void) {
    const char *word = "Level";

    if (is_palindrome(word)) {
        printf("%s is a palindrome\n", word);
    } else {
        printf("%s is not a palindrome\n", word);
    }

    return 0;
}

How It Works

Palindrome tests use the same two-pointer walk as reversal, but compare instead of swap. The source string stays unchanged—often preferable to mutating with strrev.

Example 5 — Odd vs Even Length Strings

Odd-length strings have a middle character that stays fixed during swaps.

C
#include <stdio.h>
#include <string.h>

void reverse_string(char *s) {
    size_t l = 0, r = strlen(s);
    if (r == 0) return;
    r--;
    while (l < r) {
        char t = s[l];
        s[l++] = s[r];
        s[r--] = t;
    }
}

int main(void) {
    char even[] = "abcd";
    char odd[]  = "abc";

    reverse_string(even);
    reverse_string(odd);

    printf("even (4): %s\n", even);
    printf("odd  (3): %s\n", odd);

    return 0;
}

How It Works

For "abc", the middle b meets itself when pointers cross. For even lengths, every character is swapped with a partner.

🚀 Common Use Cases

  • Interview practice — classic two-pointer string problem.
  • Palindrome helpers — reverse a copy and compare (or compare ends directly).
  • Display tricks — show text backwards in simple CLI demos.
  • Teaching — in-place mutation and swap logic.
  • Legacy code — reading Borland/MSVC strrev calls.

🧠 How strrev() Works

1

left = 0

right = strlen(str) - 1

Init
2

Swap str[left] and str[right]

Exchange outer characters.

Swap
3

Move inward

left++, right-- until left ≥ right.

Loop
=

char* str

Same buffer, characters reversed.

📝 Notes

  • Not part of ISO C—use the swap loop on GCC/Linux.
  • Modifies the string in place; copy first if needed.
  • Never pass string literals or read-only memory.
  • Empty string and single-character string are valid edge cases.
  • Reversal is byte-order only—not Unicode grapheme aware.

⚡ Optimization

Reversing is O(n) time and O(1) extra space with the two-pointer method. That is optimal for in-place reversal. Avoid allocating a second buffer unless you must preserve the original string.

Conclusion

strrev() reverses a C-string in place where the extension exists. Master the portable swap loop, copy before mutating when you need both versions, and prefer two-pointer comparison for palindrome checks without modification.

Next, learn strset() to fill an entire string with one character.

💡 Best Practices

✅ Do

  • Use a writable char[] or heap buffer
  • Print or copy the original before reversing in place
  • Use size_t or careful casts for indexes
  • Implement the two-pointer loop for portability
  • Compare both ends for palindromes without mutating

❌ Don’t

  • Call strrev on string literals
  • Assume strrev exists on every compiler
  • Expect “original” text after in-place reverse
  • Reverse UTF-8 strings expecting correct glyph order
  • Forget that return value equals the same mutated buffer

Key Takeaways

Knowledge Unlocked

Five things to remember about strrev()

Use these points when reversing strings in C.

5
Core concepts
📝 02

Two ptrs

Swap ends.

Algorithm
🔢 03

Extension

Not ISO C.

Portability
📈 04

Copy first

Keep orig.

Safety
💬 05

Palindrome

Compare.

Pattern

❓ Frequently Asked Questions

strrev() reverses the characters in a null-terminated string in place—the first character swaps with the last, and so on toward the center. It returns a pointer to the same buffer (now reversed).
No. strrev() is a compiler extension (common on Microsoft and Borland toolchains). ISO C does not define it. Portable code uses a two-pointer swap loop with strlen().
char* strrev(char* str); str must be a writable char array or heap buffer. It cannot be a string literal because literals are read-only.
No. It modifies the existing buffer. If you need to keep the original text, copy it first with strcpy or strdup before calling strrev.
Set left = 0 and right = strlen(s) - 1, then swap s[left] and s[right] while left < right, incrementing left and decrementing right each iteration.
Only on a copy of the string. Reverse the copy and strcmp it to the original, or compare characters from both ends without modifying the string—safer for interviews and production code.
Did you know?

The old reference printed “Original” and “Reversed” after one strrev call on the same buffer—both lines would show the reversed text. Always print before mutating, or reverse a copy. That small detail separates working code from confusing output in interviews.

Continue the String Functions Series

Reverse strings with strrev() concepts, then learn full-string fill with strset().

Next: strset() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful