C strrev() Function

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.
| Call | What 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
Parameters
| Parameter | Description |
|---|
| str | Required. A writable C-string. Do not pass a string literal. |
Return Value
| Return Value | Description |
|---|
| str pointer | The 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.
#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
- Use an array.
char text[] is writable; a literal is not. - Measure once.
strlen gives the last character index as len - 1. - Swap inward. Ends trade places until
left meets or passes right. - 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.
#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
- Copy first.
strcpy fills a writable buffer. - Reverse only the copy.
src stays "Hi!". - Size the destination.
copy[8] has room for the text and '\0'. - 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.
#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
- Same two ends. Walk inward like reverse, but only compare.
- Mismatch fails fast. One unequal pair means not a palindrome.
- No mutation. The original string is never changed.
- 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.
About the author
Developer, cloud engineer, and technical writer
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