C String Reversal Program


String reversal in C involves reversing the order of characters in a string. This can be achieved using different methods, including iterative and recursive approaches. Below, I will explain how to implement string reversal using both methods.

Method 1: Iterative String Reversal

In this approach, we swap characters from the beginning and end of the string, moving towards the center.

Implementation

#include <stdio.h> #include <string.h> // Function to reverse a string iteratively void reverseStringIterative(char str[]) { int left = 0; // Start pointer int right = strlen(str) - 1; // End pointer // Swap characters from left to right while (left < right) { // Swap characters char temp = str[left]; str[left] = str[right]; str[right] = temp; // Move towards the center left++; right--; } } int main() { char str[100]; // Array to hold the string // Ask user for input printf("Enter a string: "); fgets(str, sizeof(str), stdin); // Read input including spaces str[strcspn(str, "\n")] = 0; // Remove the newline character if present // Reverse the string reverseStringIterative(str); // Display the reversed string printf("Reversed string: %s\n", str); return 0; }

Explanation of Iterative Method

  1. Header Files:

    • #include <stdio.h>: Includes standard input-output functions.
    • #include <string.h>: Includes string manipulation functions.
  2. Function Definition:

    • void reverseStringIterative(char str[]): This function takes a string as input and reverses it in place.
  3. Variables:

    • left: Pointer starting from the beginning of the string.
    • right: Pointer starting from the end of the string.
  4. While Loop:

    • The loop runs as long as left is less than right.
    • Characters at positions left and right are swapped.
    • Both pointers move closer to the center.
  5. Main Function:

    • An array str is declared to hold the input string.
    • The user is prompted to enter a string using fgets to include spaces.
    • The strcspn function is used to remove any newline character from the string.
    • The reversal function is called, and the result is displayed.

Method 2: Recursive String Reversal

In this approach, we recursively reverse the string by swapping the first and last characters, then calling the function on the substring that excludes these two characters.

Implementation

#include <stdio.h> #include <string.h> // Function to reverse a string recursively void reverseStringRecursive(char str[], int left, int right) { if (left >= right) { return; // Base case: if pointers cross, return } // Swap characters char temp = str[left]; str[left] = str[right]; str[right] = temp; // Recursive call for the next pair of characters reverseStringRecursive(str, left + 1, right - 1); } int main() { char str[100]; // Array to hold the string // Ask user for input printf("Enter a string: "); fgets(str, sizeof(str), stdin); // Read input including spaces str[strcspn(str, "\n")] = 0; // Remove the newline character if present // Reverse the string reverseStringRecursive(str, 0, strlen(str) - 1); // Display the reversed string printf("Reversed string: %s\n", str); return 0; }

Explanation of Recursive Method

  1. Function Definition:

    • void reverseStringRecursive(char str[], int left, int right): This function takes a string and two indices (left and right) to reverse the string recursively.
  2. Base Case:

    • The recursion stops when the left index is greater than or equal to the right index.
  3. Swapping:

    • Characters at positions left and right are swapped.
  4. Recursive Call:

    • The function is called again with updated indices (left + 1 and right - 1).
  5. Main Function:

    • The main function is similar to the iterative example, prompting for input and calling the recursive function to reverse the string.

How to Run the Program

  1. Compile the Code: Use a C compiler like gcc to compile the code.

    gcc string_reversal.c -o string_reversal
  2. Execute the Program:

    ./string_reversal
  3. Input Data: Enter a string when prompted.

Example Input/Output

Input:

Enter a string: Hello World

Output:

Reversed string: dlroW olleH

Conclusion

String reversal is a common programming task that can be implemented using iterative or recursive methods in C. Both methods demonstrate the use of string manipulation, pointer arithmetic, and control flow in the C programming language.