Programmers commonly face the problem of reversing a string to test their understanding of arrays, pointers, and string manipulation in C. In this article, we’ll explore three ways for “String Reverse in C”, complete with explanations and example code for each method.
Learn how to write a program for “String Reverse in C”
Method 1) String Reverse in C using a Temporary Array
One of the simplest ways to reverse a string is by using a temporary array. This method involves copying the original string into a temporary array in reverse order and then copying the reversed string back to the original array.
Steps:
- Find the length of the string.
- Use a loop to copy characters from the original string to the temporary array in reverse order.
- Copy the reversed string back to the original string.
Code Example
#include <stdio.h> #include <string.h> void reverseString(char str[]) { int n = strlen(str); char temp[n + 1]; for (int i = 0; i < n; i++) { temp[i] = str[n - 1 - i]; } temp[n] = '\0'; strcpy(str, temp); } int main() { char str[] = "Hello, World!"; reverseString(str); printf("Reversed String: %s\n", str); return 0; }
Explanation of the Code
strlen(str)
calculates the length of the string.- A temporary array
temp
is created to hold the reversed string. - A for loop populates the
temp
array with characters from the original string in reverse order. - The reversed string in
temp
is then copied back to the original string usingstrcpy
.
Method 2) String Reverse in C using Two-Pointer Technique
The two-pointer technique is an efficient way to reverse a string in place. This method uses two pointers, one starting from the beginning and the other from the end of the string, and swaps characters until the pointers meet in the middle.
Steps:
- Initialize two pointers, one at the start and the other at the end of the string.
- Swap the characters at these pointers.
- Move the pointers towards each other and repeat the process until they meet.
Code Example
#include <stdio.h> #include <string.h> void reverseString(char str[]) { int left = 0; int right = strlen(str) - 1; char temp; while (left < right) { temp = str[left]; str[left] = str[right]; str[right] = temp; left++; right--; } } int main() { char str[] = "Hello, World!"; reverseString(str); printf("Reversed String: %s\n", str); return 0; }
Explanation of the Code
- Initialize two pointers, left and right, to the start and end of the string, respectively.
- Swap the characters at these pointers using a temporary variable temp.
- The pointers are moved towards each other (
left
incremented andright
decremented) and the process is repeated untilleft
is no longer less thanright
.
Method 3) String Reverse in C using Recursion
Recursion provides a clean and elegant way to reverse a string. This method involves swapping characters from the ends towards the center, similar to the two-pointer technique but uses recursive function calls instead of a loop.
Steps:
- Define a recursive function that takes the string, a start index, and an end index as parameters.
- Swap the characters at the start and end indices.
- Recursively call the function with updated indices, moving towards the center.
Code Example
#include <stdio.h> #include <string.h> void reverseStringRecursive(char str[], int start, int end) { if (start >= end) { return; } char temp = str[start]; str[start] = str[end]; str[end] = temp; reverseStringRecursive(str, start + 1, end - 1); } int main() { char str[] = "Hello, World!"; reverseStringRecursive(str, 0, strlen(str) - 1); printf("Reversed String: %s\n", str); return 0; }
Explanation of the Code
- The recursive function
reverseStringRecursive
takes the string, astart
index, and anend
index. - It swaps the characters at the
start
andend
indices. - The function calls itself with
start + 1
andend - 1
until thestart
index is no longer less than theend
index.