C Online Compiler
Example: String Reversal using Recursion in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// String Reversal using Recursion #include <stdio.h> #include <string.h> void reverseStringRecursive(char *str, int start, int end) { char temp; // Base case: if start index is greater than or equal to end index, stop recursion if (start >= end) { return; } // Step 1: Swap characters at 'start' and 'end' temp = str[start]; str[start] = str[end]; str[end] = temp; // Step 2: Recursively call the function for the inner substring reverseStringRecursive(str, start + 1, end - 1); } int main() { char myString[] = "algorithms"; printf("Original string: %s\n", myString); // Call the recursive function with initial start and end indices reverseStringRecursive(myString, 0, strlen(myString) - 1); printf("Reversed string: %s\n", myString); return 0; }
Output
Clear
ADVERTISEMENTS