To reverse a string in C, many textbooks use the strrev() function from <string.h>. It is quick, but there is an important catch: strrev() is not part of standard C. It works on Turbo C and on Microsoft’s compiler, but GCC on Linux and most online compilers do not have it. This page shows the strrev() method and a portable method that works everywhere.
Reverse a String Using strrev()
#include <stdio.h>
#include <string.h>
int main(void) {
char str[] = "GyaniPandit";
strrev(str);
printf("Reversed string: %sn", str);
return 0;
}
Output (on compilers that provide strrev):
Reversed string: tidnaPinayG
strrev() reverses the string in place, meaning it changes the original array. It also returns a pointer to the same string.
On GCC (Linux, macOS, most online compilers) this program does not build. You will see a warning about an implicit declaration of strrev followed by a linker error such as “undefined reference to `strrev`”. On Microsoft’s compiler the function is also available as _strrev().
Reverse a String Without strrev() (Works Everywhere)
Swap the first and last characters, then the second and second-last, and so on until you reach the middle:
#include <stdio.h>
#include <string.h>
void reverse(char *s) {
int i = 0;
int j = (int)strlen(s) - 1;
while (i < j) {
char temp = s[i];
s[i] = s[j];
s[j] = temp;
i++;
j--;
}
}
int main(void) {
char str[] = "GyaniPandit";
reverse(str);
printf("Reversed string: %sn", str);
return 0;
}
Output:
Reversed string: tidnaPinayG
How it works
istarts at the first character andjat the last one (strlen(s) - 1).- The two characters are swapped using a temporary variable.
imoves right andjmoves left. The loop stops when they meet in the middle, so each pair is swapped only once.
For a string of length n, this does about n/2 swaps, so it runs in O(n) time and needs no extra array.
Reverse a String Entered by the User
#include <stdio.h>
#include <string.h>
void reverse(char *s) {
int i = 0;
int j = (int)strlen(s) - 1;
while (i < j) {
char temp = s[i];
s[i++] = s[j];
s[j--] = temp;
}
}
int main(void) {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof str, stdin);
str[strcspn(str, "n")] = ' '; /* remove the newline fgets keeps */
reverse(str);
printf("Reversed string: %sn", str);
return 0;
}
Output:
Enter a string: hello world
Reversed string: dlrow olleh
fgets() is used instead of gets() because gets() cannot limit the input size and was removed from the C standard in C11.
Use It: Check Whether a String Is a Palindrome
#include <stdio.h>
#include <string.h>
int main(void) {
char word[] = "madam";
char copy[sizeof word];
strcpy(copy, word);
/* reverse the copy */
for (int i = 0, j = (int)strlen(copy) - 1; i < j; i++, j--) {
char t = copy[i];
copy[i] = copy[j];
copy[j] = t;
}
if (strcmp(word, copy) == 0)
printf("%s is a palindromen", word);
else
printf("%s is not a palindromen", word);
return 0;
}
Output:
madam is a palindrome
Common Mistakes
- Reversing a string literal through a pointer:
char *s = "hello"; strrev(s);tries to modify read-only memory and can crash. Use an array:char s[] = "hello"; - Including the
' 'in the swap: startjatstrlen(s) - 1, notstrlen(s), or the terminator moves to the front and the string looks empty. - Forgetting the newline from
fgets(): it would become the first character after reversing.
Frequently Asked Questions
Is strrev() a standard C function?
No. It is a non-standard extension found in Turbo C and Microsoft’s C library (as _strrev). Standard C and GCC do not provide it.
Which header file has strrev()?
<string.h>, on the compilers that support it.
How do I reverse a string in C without any library function?
Find the length with a loop, then swap characters from both ends towards the middle, as in the reverse() function above.
Related: How to find ASCII value in C