Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is the Meaning of Reverse a String?
3.
Different Ways to Reverse a String in C
3.1.
Reverse a String using the strrev() Function
3.2.
C
3.3.
Reverse a String without using the Library Function
3.4.
C
3.5.
Reverse a String using for Loop
3.6.
C
3.7.
Reverse a String using a While Loop
3.8.
C
3.9.
Reverse a String using the Recursion Function
3.10.
C
3.11.
Reverse a String using Pointers
3.12.
C
3.13.
Reverse a String using Stack
3.14.
C
4.
Frequently Asked Questions
4.1.
How do you reverse a string in C? 
4.2.
How to reverse each word in a string in C?
4.3.
Does reverse () work on strings?
4.4.
How do you reverse a string without a reverse function?
5.
Conclusion
Last Updated: Mar 28, 2024
Easy

Reverse a String in C

Author Gunjan
1 upvote
gp-icon
Data structures & algorithms (Beginner to Intermediate)
Free guided path
13 chapters
99+ problems
gp-badge
Earn badges and level up

Introduction

Reversing a string is an important problem that is famous across many competitive coding problem banks. Before that, what is a string?

reverse a string in c

A string is a set of characters stored in an array, or we can also define it as an array of characters, followed by a null character. The task is to reverse the string. In C language, there are various methods to perform reverse string. Let us first know what is Reverse A String In C.

What is the Meaning of Reverse a String?

In C programming language, reversing a string involves changing the order of its characters so that the last character becomes the first, the second-last character becomes the second, and so on. 
To reverse a string means to change its order so that the characters that were initially at the end of the string are now at the beginning and vice versa.
For example, if the original string is "ninjas", the reversed string would be "sajnin".
Reversing a string is a common task in programming and can be helpful in various applications such as searching and sorting algorithms.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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

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;
}


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

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;
}

 

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

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;
}

 

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

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;
}

 

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

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);
}

 

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

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;
}


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

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;
}


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.

Conclusion

This article taught you about different ways of reversing a string in C programming language. We discussed methods like recursion, two-pointer algorithm, built-in functions, and more.

For more practice problems on strings, please visit the Coding Ninjas Studio section. The platform has a lot of practice problems, divided among different important concepts. Moreover, you get to practice the top interview problems by the top product-based companies.

Guided path
Free
gridgp-icon
Data structures & algorithms (Beginner to Intermediate)
13 chapters
109+ Problems
gp-badge
Earn badges and level up
Live masterclass