Reverse a String in C

To reverse a string in C, we can use various approaches such as looping, recursion, and built-in functions. The most common ways include using a loop to swap characters, recursion to reverse the string in a function call stack, or using strrev() (not part of standard C but available in some compilers).

In this tutorial, we will cover multiple methods to reverse a string with clear explanations and examples.


Examples of Reversing a String in C

1. Reversing a String Using a Loop

In this method, we will use a for loop to swap characters from the beginning and end of the string until we reach the middle.

main.c

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

void reverseString(char str[]) {
    int length = strlen(str);
    int start = 0, end = length - 1;
    char temp;

    while (start < end) {
        temp = str[start];
        str[start] = str[end];
        str[end] = temp;
        start++;
        end--;
    }
}

int main() {
    char str[] = "Hello";
    
    reverseString(str);
    printf("Reversed string: %s\n", str);

    return 0;
}

Explanation:

  1. We include string.h for the strlen() function.
  2. The function reverseString() takes a character array (str) as input.
  3. We find the length of the string using strlen().
  4. We initialize start to 0 and end to length - 1.
  5. Using a while loop, we swap characters from the start and end until the middle is reached.
  6. The modified string is printed in the main() function.

Output:

Reversed string: olleH

2. Reversing a String Using Recursion

In this method, we use recursion to reverse the string by swapping characters and calling the function with a smaller substring.

main.c

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

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

    char temp = str[start];
    str[start] = str[end];
    str[end] = temp;

    reverseRecursive(str, start + 1, end - 1);
}

int main() {
    char str[] = "World";
    
    reverseRecursive(str, 0, strlen(str) - 1);
    printf("Reversed string: %s\n", str);

    return 0;
}

Explanation:

  1. The function reverseRecursive() takes three parameters: str, start, and end.
  2. If start is greater than or equal to end, we return (base case for recursion).
  3. We swap the characters at start and end.
  4. We recursively call reverseRecursive() with start + 1 and end - 1.
  5. The reversed string is printed in the main() function.

Output:

Reversed string: dlroW

3. Reversing a String Using strrev()

Some compilers provide the non-standard function strrev() to reverse a string in place.

main.c

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

int main() {
    char str[] = "Programming";
    
    printf("Reversed string: %s\n", strrev(str));

    return 0;
}

Explanation:

  1. We include string.h for the strrev() function.
  2. The function strrev(str) reverses the string in place.
  3. We print the reversed string using printf().

Output:

Reversed string: gnimmargorP

Conclusion

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

  1. Loop Method: Swaps characters from both ends of the string.
  2. Recursion: Calls itself to swap characters until the middle is reached.
  3. strrev() Function: Uses a built-in function (not standard C).

Each method has its advantages. Loops are efficient, recursion is useful for understanding call stacks, and strrev() is the simplest if available in your compiler.