Different Ways to Reverse a String in C
Let’s discuss some of the methods to reverse a string in C.:
- Reverse a string using the strrev() function
- Reverse a string without using the library function
- Reverse a string using for loop
- Reverse a string using a while loop
- Reverse a string using the Recursion function
- Reverse a string using pointers
- Reverse a string using stack
Reverse a String using the strrev() Function
The strrev() function directly reverses the given string, and this function is present in string.h library. The code for this function is inbuilt in the library, we need to just give a string as the input to the function.
C
#include <stdio.h>
#include <string.h>
int main()
{
char s[100]; //string declaration
printf("Enter a string:");
gets(s); //input
strrev(s); //reversing string
printf("The reverse of the string: %s\n", s);
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output:
Enter a string: Hello
The reverse of the string: olleH
Reverse a String without using the Library Function
To reverse a string without using the library function we can take help of an additional string. We will start iterating from the beginning of the string and with the help of the other string, we will copy the other string in the reverse order to the original string.
C
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
printf("Enter a string:");
// read string
scanf("%s", str);
char temp[100];
// copy the string into temp
strcpy(temp, str);
int length = strlen(str);
int start = 0, end = length - 1;
while (start < end) {
char temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
printf("The reverse of the string is \"%s\".\n", str);
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output:
Enter a string:hello
The reverse of the string is "olleh".
Reverse a String using for Loop
We can also reverse a string using a for loop approach. In this approach we take two pointers, one at the starting index and one at the index. We swap the characters and increase the starting pointer and decrease the end pointer. We iterate until the starting pointer becomes greater than the end pointer.
C
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
printf("Enter a string:");
// read string
scanf("%s", str);
int length = strlen(str);
for(int start =0, end=length-1; start<end; start++,end--) {
char temp = str[i];
str[i] = str[j];
str[j] = temp;
}
printf("The reverse of the string is \"%s\".\n", str);
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output:
Enter a string:hello
The reverse of the string is "olleh".
Reverse a String using a While Loop
The same approach of using two pointers which we saw above using for loop can also be implemented using the while loop.
C
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
printf("Enter a string:");
scanf("%s", str); // read string
int length = strlen(str);
int start=0, end=length-1;
while (start < end) {
char temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
printf("The reverse of the string is \"%s\".\n", str);
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output:
Enter a string:hello
The reverse of the string is "olleh".
Reverse a String using the Recursion Function
As we all know, Recursion is the concept where we perform an operation on a single element and the recursive function call returns the updated object.
To reverse the string:
- Remove one character from the string.
- Call the recursive function.
- Insert the removed character, such that the reverse operation holds.
C
#include <stdio.h>
#include <string.h>
void reverse(char*, int, int);
int main()
{
char a[100];
gets(a); //read string
reverse(a, 0, strlen(a)-1);
printf("%s\n", a);
return 0;
}
void reverse(char *x, int begin, int end)
{
char c;
if (begin >= end)
return;
c = *(x+begin);
*(x+begin) = *(x+end);
*(x+end) = c;
reverse(x, ++begin, --end);
}

You can also try this code with Online C Compiler
Run Code
Output:
CodingNinjas
sajniNgnidoC
Reverse a String using Pointers
The idea is to swap the begin and end pointers of the string. Instead of swapping the elements, here we will swap the pointers where the addresses of the input string are stored.
C
#include<stdio.h>
int string_length(char*);
void reverse(char*);
int main()
{
char s[100];
printf("Enter a string:");
gets(s);
reverse(s);
printf("The reverse of the string is \"%s\".\n", s);
return 0;
}
void reverse(char *s)
{
int length, c;
char *begin, *end, temp;
length = string_length(s);
begin = s;
end = s;
for (c = 0; c < length - 1; c++)
end++;
for (c = 0; c < length/2; c++)
{
temp = *end;
*end = *begin;
*begin = temp;
begin++;
end--;
}
}
int string_length(char *pointer)
{
int c = 0;
while( *(pointer + c) != '\0' )
c++;
return c;
}

You can also try this code with Online C Compiler
Run Code
Output:
Enter a string: CodingNinjas
The reverse of the string is "sajniNgnidoC".
Reverse a String using Stack
Stack is one of the simpler data structures that can be used for reverse operations. Since stack follows Last in First out approach. We can use this property to reverse the string.
The idea is to push all the string elements into a stack, and pop them one by one. This way the popped elements are received in reverse order.
C
#include <stdio.h>
#include <string.h>
#define max 100
int top,stack[max];
void push(char x){
// Push(Inserting Element in stack) operation
if(top == max-1){
printf("stack overflow");
} else {
stack[++top]=x;
}
}
void pop(){
// Pop (Removing element from stack)
printf("%c",stack[top--]);
}
int main()
{
char str[]="Coding Ninjas";
int len = strlen(str);
int i;
for(i=0;i<len;i++)
push(str[i]);
for(i=0;i<len;i++)
pop();
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output:
The reversed string is: sajniNgnidoC
Also see, Palindrome string
Frequently Asked Questions
How do you reverse a string in C?
Reversing a string can be done in many different ways. The naive approach is to use a loop and traverse it from the last. The other methods are using the strrev() function, using pointers, recursion, and stack.
How to reverse each word in a string in C?
Start the program. Take a string as input. Declare a function to reverse a string which swaps the character with their corresponding characters. Call the function. Print the Reversed String. End the Program.
Does reverse () work on strings?
Yes, we have inbuilt function that works for strings reversal.
How do you reverse a string without a reverse function?
To reverse a string in C without using a reverse function like strrev(), you can implement your own algorithm that swaps the characters in the string. You can also use the pointer, recursion, and other methods as well.
How does reverse () work?
The reverse() function in C++ is used to reverse the order of elements in a container, such as an array or vector. It operates by swapping elements from the beginning and end, moving towards the center, until all elements are reversed. This function is typically found in the <algorithm> header.
Conclusion
In conclusion, reversing a string in C can be achieved by swapping characters from the beginning and end of the string, moving towards the center. This method is efficient and commonly implemented using a simple loop. It provides a straightforward solution for string manipulation in C.