String Reverse in C

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:

  1. Find the length of the string.
  2. Use a loop to copy characters from the original string to the temporary array in reverse order.
  3. 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 using strcpy.

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:

  1. Initialize two pointers, one at the start and the other at the end of the string.
  2. Swap the characters at these pointers.
  3. 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 and right decremented) and the process is repeated until left is no longer less than right.

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:

  1. Define a recursive function that takes the string, a start index, and an end index as parameters.
  2. Swap the characters at the start and end indices.
  3. 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, a start index, and an end index.
  • It swaps the characters at the start and end indices.
  • The function calls itself with start + 1 and end - 1 until the start index is no longer less than the end index.

You can reverse a string in C using various methods, each showcasing different aspects of the language. The temporary array method is easy to understand, the two-pointer technique is efficient and avoids extra memory usage, and the recursive method offers a clean, elegant solution. Understanding and implementing these methods can enhance your string manipulation skills in C programming.

Categories C

Leave a Comment