C strrev() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
string.h

Definition

In C, strrev() reverses a writable C-string in the same buffer. The original order is replaced. It is a common extension and is not part of ISO C.

You pass one writable string. It is required. The result is the same pointer. Prefer a portable swap loop when you need standard C.

Syntax

Pass a writable char array. Start with this call.

C
strrev(word);
CallWhat it does
strrev(word)Reverses the characters in word, in place.

Not ISO C. Never pass a string literal. The examples on this page show a portable reverse loop.

Library declarations
C
char *strrev(char *str);

Parameters

ParameterDescription
strRequired. A writable C-string. Do not pass a string literal.

Return Value

Return ValueDescription
str pointerThe same pointer you passed in.

C Examples

Example 1 — Portable In-Place Reverse

Swap from both ends. This is the standard way to reverse a C-string without relying on strrev.

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

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

    if (s == NULL || s[0] == '\0') {
        return;
    }

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

int main(void) {
    char text[] = "code";

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

    return 0;
}

How It Works

  1. Use an array. char text[] is writable; a literal is not.
  2. Measure once. strlen gives the last character index as len - 1.
  3. Swap inward. Ends trade places until left meets or passes right.
  4. Terminator stays. Only characters move; '\0' remains at the end.

Example 2 — Reverse a Copy, Keep the Original

Reverse destroys the buffer. Copy first when you still need the source text.

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

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

    if (s == NULL || s[0] == '\0') {
        return;
    }

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

int main(void) {
    const char *src = "Hi!";
    char copy[8];

    strcpy(copy, src);
    reverse_string(copy);

    printf("src  = %s\n", src);
    printf("copy = %s\n", copy);

    return 0;
}

How It Works

  1. Copy first. strcpy fills a writable buffer.
  2. Reverse only the copy. src stays "Hi!".
  3. Size the destination. copy[8] has room for the text and '\0'.
  4. Same idea as strrev. Extension APIs also mutate the buffer you pass.

Example 3 — Palindrome Check Without Mutating

Compare characters from both ends. No reverse, no extra buffer.

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

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

    if (s == NULL) {
        return 0;
    }

    right = strlen(s);
    if (right == 0) {
        return 1;
    }
    right--;

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

int main(void) {
    printf("%s\n", is_palindrome("level") ? "yes" : "no");
    printf("%s\n", is_palindrome("code") ? "yes" : "no");

    return 0;
}

How It Works

  1. Same two ends. Walk inward like reverse, but only compare.
  2. Mismatch fails fast. One unequal pair means not a palindrome.
  3. No mutation. The original string is never changed.
  4. Better than reverse+strcmp. Saves a copy and still uses O(1) extra space.

Frequently Asked Questions

strrev() reverses a writable C-string in place.
str is required. It must be a writable C-string, not a string literal.
The same str pointer you passed in.
No. It is an extension. Use a portable swap loop for ISO C.
No. String literals must not be modified.

Next: strset()

Fill every character of a C-string with one value.

strset() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

9 people found this page helpful