VOOZH about

URL: https://www.geeksforgeeks.org/c/how-to-reverse-a-string-in-c/

⇱ How to Reverse a String in C? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Reverse a String in C?

Last Updated : 23 Jul, 2025

In C, a string is a sequence of characters terminated by a null character (\0). Reversing a string means changing the order of the characters such that the characters at the end of the string come at the start and vice versa. In this article, we will learn how to reverse a string in C.

Example:

Input:
char myStr[] = "Hello";

Output:
Reversed String: olleH

Reverse a String in C

To reverse a in C, we can use a approach in which we will use two pointers pointing to the first and last index. In every iteration, swap the characters at these positions and move the pointers toward each other until they meet or cross to get the reversed string.

Approach:

  1. Declare two pointers, start(pointing to the beginning) and end (pointing to the end).
  2. Swap the characters at the positions pointed by start and end.
  3. Increment start and decrement end.
  4. Repeat steps 2 and 3 until start is greater than or equal to end.

C Program to Reverse a String

The below program demonstrates how we can reverse a string in C.


Output
Original string: Hello, World!
Reversed string: !dlroW ,olleH

Time Complexity: O(N), where N is the length of the string. 
Auxiliary Space: O(1)

Comment