Reverse a String using Loops in C

In C, we can reverse a string using different looping techniques such as the for loop and the while loop. Reversing a string involves iterating through the characters from the end to the beginning and storing them in a new string or swapping characters in-place.


Examples to Reverse a String Using Loops

1. Reverse a String Using a for Loop

In this example, we will reverse a string by iterating from the last character to the first and storing it in a new string.

main.c

</>
Copy
#include <stdio.h>
#include <string.h>

int main() {
    char str[] = "Hello";
    int len = strlen(str);
    char rev[len + 1];

    // Using for loop to reverse the string
    for (int i = 0; i < len; i++) {
        rev[i] = str[len - i - 1];
    }
    rev[len] = '\0'; // Null terminator

    printf("Reversed String: %s\n", rev);

    return 0;
}

Explanation:

  1. We declare the original string str[] and determine its length using strlen().
  2. We create an empty array rev[] to store the reversed string.
  3. Using a for loop, we iterate from the last character of str to the first and copy each character to rev[].
  4. We add a null terminator '\0' at the end to mark it as a valid string.
  5. Finally, we print the reversed string using printf().

Output:

Reversed String: olleH

2. Reverse a String In-Place Using a while Loop

In this example, we will reverse a string in-place by swapping characters from both ends using a while loop.

main.c

</>
Copy
#include <stdio.h>
#include <string.h>

void reverseString(char str[]) {
    int left = 0, right = strlen(str) - 1;
    char temp;

    // Using while loop to swap characters
    while (left < right) {
        temp = str[left];
        str[left] = str[right];
        str[right] = temp;
        left++;
        right--;
    }
}

int main() {
    char str[] = "World";

    reverseString(str);
    printf("Reversed String: %s\n", str);

    return 0;
}

Explanation:

  1. We define a function reverseString() that takes a character array as input.
  2. We initialize two indices: left starting at the first character and right starting at the last character.
  3. Inside a while loop, we swap the characters at left and right and then move them towards the center.
  4. The process continues until left is no longer less than right.
  5. Since we modify the string in-place, we directly print the modified str in the main() function.

Output:

Reversed String: dlroW

3. Reverse a String Using Recursion

In this example, we will reverse a string using recursion, which repeatedly swaps characters until the entire string is reversed.

main.c

</>
Copy
#include <stdio.h>
#include <string.h>

void reverseRecursive(char str[], int left, int right) {
    if (left >= right) return;

    // Swap characters
    char temp = str[left];
    str[left] = str[right];
    str[right] = temp;

    // Recursive call
    reverseRecursive(str, left + 1, right - 1);
}

int main() {
    char str[] = "Recursion";

    reverseRecursive(str, 0, strlen(str) - 1);
    printf("Reversed String: %s\n", str);

    return 0;
}

Explanation:

  1. We define a recursive function reverseRecursive() that swaps characters from both ends.
  2. We use a base case: if left is greater than or equal to right, we stop recursion.
  3. We swap characters at positions left and right.
  4. We call reverseRecursive() again, moving left forward and right backward.
  5. The function continues until the entire string is reversed, then the modified string is printed.

Output:

Reversed String: noisruceR

Conclusion

In this tutorial, we explored different ways to reverse a string in C:

  1. Using a for loop – Copying characters in reverse order.
  2. Using a while loop – Swapping characters in-place.
  3. Using recursion – Swapping characters using a function call stack.

Each method has its own advantages depending on the use case.