Program to Reverse a String Using Recursion
- Write a program to Reverse a String Using Recursion in C
- Write a program to Reverse a String Using Recursion in C++
- Write a program to Reverse a String Using Recursion in Python
- Write a program to Reverse a String Using Recursion in PHP
- Write a program to Reverse a String Using Recursion in Java
- Write a program to Reverse a String Using Recursion in Java Script
- Write a program to Reverse a String Using Recursion in C#
Explanation:
The standard method for using recursion to reverse a string is to process it from beginning to finish, adding characters in the opposite order to create the reversed string.
Here is a detailed explanation of the reasoning:
Base Case:
- If the string is empty or has only one character (i.e., its length is 0 or 1), return the string itself because the reversed string is the same as the original string.
Recursive Case:
- The idea is to take the first character of the string and append it to the reversed version of the remaining substring.
- The function will call itself with the substring (excluding the first character), and append the first character to the result of the recursive call.
Recursive Call:
- The recursion continues by reducing the string size until it reaches the base case.
Program to Reverse a String Using Recursion
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
#include <stdio.h> #include <string.h> void reverse_string(char str[], int start, int end) { if (start >= end) return; // Swap characters at start and end char temp = str[start]; str[start] = str[end]; str[end] = temp; reverse_string(str, start + 1, end - 1); } int main() { char str[100]; printf("Enter a string: "); fgets(str, sizeof(str), stdin); // Remove newline character if present str[strcspn(str, "\n")] = '\0'; int length = strlen(str); reverse_string(str, 0, length - 1); printf("Reversed string is: %s\n", str); return 0; }
#include <iostream> #include <string> using namespace std; void reverse_string(string &str, int start, int end) { if (start >= end) return; // Swap characters at start and end swap(str[start], str[end]); reverse_string(str, start + 1, end - 1); } int main() { string str; cout << "Enter a string: "; getline(cin, str); reverse_string(str, 0, str.length() - 1); cout << "Reversed string is: " << str << endl; return 0; }
def reverse_string(str): if len(str) == 0: return str return reverse_string(str[1:]) + str[0] str = input("Enter a string: ") print(f"Reversed string is: {reverse_string(str)}")
<?php function reverse_string($str) { if (strlen($str) == 0) return $str; return reverse_string(substr($str, 1)) . $str[0]; } echo "Enter a string: "; $str = trim(fgets(STDIN)); echo "Reversed string is: " . reverse_string($str) . "\n"; ?>
import java.util.Scanner; public class Main { public static String reverse_string(String str) { if (str.length() == 0) return str; return reverse_string(str.substring(1)) + str.charAt(0); } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a string: "); String str = scanner.nextLine(); System.out.println("Reversed string is: " + reverse_string(str)); } }
function reverse_string(str) { if (str.length === 0) return str; return reverse_string(str.slice(1)) + str.charAt(0); } const str = prompt("Enter a string:"); console.log("Reversed string is: " + reverse_string(str));
using System; class Program { static string ReverseString(string str) { if (str.Length == 0) return str; return ReverseString(str.Substring(1)) + str[0]; } static void Main() { Console.Write("Enter a string: "); string str = Console.ReadLine(); Console.WriteLine("Reversed string is: " + ReverseString(str)); } }