Reverse string using recursion

Here given code implementation process.

// Reverse string using recursion in java
class MyString
{
	//This is reversing the string elements recursively
	public String reverse_str(String text, int location)
	{
		//Base condition to stop the recursion process
		if (location >= 0)
		{
			//Recursive function call
			return text.charAt(location) + reverse_str(text, location - 1);
		}
		//When no character remaining
		return "";
	}
	//This is handling the request process of reverse string elements
	public String reverse(String text)
	{
		//Display given string elements
		System.out.println("Before Text : [" + text + "]");
		return reverse_str(text, text.length() - 1);
	}
	public static void main(String[] args)
	{
		MyString obj = new MyString();
		String text = "ABCDE";
		text = obj.reverse(text);
		//After reverse	
		System.out.println("After Text  : [" + text + "]\n");
		text = obj.reverse("654A321");
		//After reverse	
		System.out.println("After Text  : [" + text + "]\n");
	}
}

Output

Before Text : [ABCDE]
After Text  : [EDCBA]

Before Text : [654A321]
After Text  : [123A456]
//Include header file
#include <iostream>
#include<string.h>

using namespace std;

// Reverse string using recursion in C++
class MyString
{
	public:
		//This is reversing the string elements recursively
		string reverse_str(string text, int location)
		{
			//Base condition to stop the recursion process
			if (location >= 0)
			{
				//Recursive function call
				return text[location] + this->reverse_str(text, location - 1);
			}
			//When no character remaining
			return "";
		}
	//This is handling the request process of reverse string elements
	string reverse(string text)
	{
		//Display given string elements
		cout << "Before Text : [" << text << "]";
		return this->reverse_str(text, text.size() - 1);
	}
};
int main()
{
	MyString obj = MyString();
	string text = "ABCDE";
	text = obj.reverse(text);
	//After reverse	
	cout << "\nAfter Text  : [" << text << "]\n";
	text = obj.reverse("654A321");
	//After reverse	
	cout << "\nAfter Text  : [" << text << "]\n";
	return 0;
}

Output

Before Text : [ABCDE]
After Text  : [EDCBA]
Before Text : [654A321]
After Text  : [123A456]
//Include namespace system
using System;
// Reverse string using recursion in C#
class MyString
{
	//This is reversing the string elements recursively
	public String reverse_str(String text, int location)
	{
		//Base condition to stop the recursion process
		if (location >= 0)
		{
			//Recursive function call
			return text[location] + reverse_str(text, location - 1);
		}
		//When no character remaining
		return "";
	}
	//This is handling the request process of reverse string elements
	public String reverse(String text)
	{
		//Display given string elements
		Console.WriteLine("Before Text : [" + text + "]");
		return reverse_str(text, text.Length - 1);
	}
	public static void Main(String[] args)
	{
		MyString obj = new MyString();
		String text = "ABCDE";
		text = obj.reverse(text);
		//After reverse	
		Console.WriteLine("After Text  : [" + text + "]\n");
		text = obj.reverse("654A321");
		//After reverse	
		Console.WriteLine("After Text  : [" + text + "]\n");
	}
}

Output

Before Text : [ABCDE]
After Text  : [EDCBA]

Before Text : [654A321]
After Text  : [123A456]
<?php
// Reverse string using recursion in Php
class MyString
{
	//This is reversing the string elements recursively
	public	function reverse_str($text, $location)
	{
		//Base condition to stop the recursion process
		if ($location >= 0)
		{
			//Recursive function call
			return $text[$location] . $this->reverse_str($text, $location - 1);
		}
		//When no character remaining
		return "";
	}
	//This is handling the request process of reverse string elements
	public	function reverse($text)
	{
		echo "Before Text : [". $text ."]";
		return $this->reverse_str($text, strlen($text) - 1);
	}
}

function main()
{
	$obj = new MyString();
	$text = "ABCDE";
	$text = $obj->reverse($text);
	echo "\nAfter Text  : [". $text ."]\n";
	$text = $obj->reverse("654A321");
	echo "\nAfter Text  : [". $text ."]\n";
}
main();

Output

Before Text : [ABCDE]
After Text  : [EDCBA]
Before Text : [654A321]
After Text  : [123A456]
// Reverse string using recursion in Node Js
class MyString
{
	//This is reversing the string elements recursively
	reverse_str(text, location)
	{
		//Base condition to stop the recursion process
		if (location >= 0)
		{
			//Recursive function call
			return text[location] + this.reverse_str(text, location - 1);
		}
		//When no character remaining
		return "";
	}
	//This is handling the request process of reverse string elements
	reverse(text)
	{
		console.log("Before Text : [" + text + "]");
		return this.reverse_str(text, text.length - 1);
	}
}

function main()
{
	var obj = new MyString();
	var text = "ABCDE";
	text = obj.reverse(text);
	console.log("After Text  : [" + text + "]\n");
	text = obj.reverse("654A321");
	process.stdout.write("After Text  : [" + text + "]\n");
}
main();

Output

Before Text : [ABCDE]
After Text  : [EDCBA]

Before Text : [654A321]
After Text  : [123A456]
#  Reverse string using recursion in Python 3
class MyString :
	# This is reversing the string elements recursively
	def reverse_str(self, text, location) :
		# Base condition to stop the recursion process
		if (location >= 0) :
			# Recursive function call
			return text[location] + self.reverse_str(text, location - 1)
		
		# When no character remaining
		return ""
	
	# This is handling the request process of reverse string elements
	def reverse(self, text) :
		print("Before Text : [", text ,"]")
		return self.reverse_str(text, len(text) - 1)
	

def main() :
	obj = MyString()
	text = "ABCDE"
	text = obj.reverse(text)
	print("After Text  : [", text ,"]\n")
	text = obj.reverse("654A321")
	print("After Text  : [", text ,"]\n")

if __name__ == "__main__": main()

Output

Before Text : [ ABCDE ]
After Text  : [ EDCBA ]

Before Text : [ 654A321 ]
After Text  : [ 123A456 ]
#  Reverse string using recursion in Ruby
class MyString

	# This is reversing the string elements recursively
	def reverse_str(text, location)
	
		# Base condition to stop the recursion process
		if (location >= 0)
		
			# Recursive function call
			return text[location] + self.reverse_str(text, location - 1)
		end
		# When no character remaining
		return ""
	end
	# This is handling the request process of reverse string elements
	def reverse(text)
	
		# Display given string elements
		print("Before Text : [", text ,"]")
		return self.reverse_str(text, text.length() - 1)
	end
end
def main()

	obj = MyString.new()
	text = "ABCDE"
	text = obj.reverse(text)
	# After reverse	
	print("\nAfter Text  : [", text ,"]\n")
	text = obj.reverse("654A321")
	# After reverse	
	print("\nAfter Text  : [", text ,"]\n")
end
main()

Output

Before Text : [ABCDE]
After Text  : [EDCBA]
Before Text : [654A321]
After Text  : [123A456]
// Reverse string using recursion in Scala
class MyString
{
	//This is reversing the string elements recursively
	def reverse_str(text: String, location: Int): String = {
		//Base condition to stop the recursion process
		if (location >= 0)
		{
			//Recursive function call
			return ""+text(location) + reverse_str(text, location - 1);
		}
		//When no character remaining
		return "";
	}
	//This is handling the request process of reverse string elements
	def reverse(text: String): String = {
		//Display given string elements
		print("Before Text : [" + text + "]");
		return reverse_str(text, text.length() - 1);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyString = new MyString();
		var text: String = "ABCDE";
		text = obj.reverse(text);
		//After reverse	
		print("\nAfter Text  : [" + text + "]\n");
		text = obj.reverse("654A321");
		//After reverse	
		print("\nAfter Text  : [" + text + "]\n");
	}
}

Output

Before Text : [ABCDE]
After Text  : [EDCBA]
Before Text : [654A321]
After Text  : [123A456]
// Reverse string using recursion in Swift
class MyString
{
	//This is reversing the string elements recursively
	func reverse_str(_ arr_str: [Character], _ location: Int) -> String
	{
		//Base condition to stop the recursion process
		if (location >= 0)
		{
			//Recursive function call
			return String(arr_str[location]) + self.reverse_str(arr_str, location - 1);
		}
		//When no character remaining
		return "";
	}
	//This is handling the request process of reverse string elements
	func reverse(_ text: String) -> String
	{
        let arr_str = Array(text);
		print("Before Text : [", text ,"]");
		return self.reverse_str(arr_str, text.count - 1);
	}
}
func main()
{
	let obj: MyString = MyString();
	var text: String = "ABCDE";
	text = obj.reverse(text);
	print("After Text  : [", text ,"]\n");
	text = obj.reverse("654A321");
	print("After Text  : [", text ,"]\n");
}
main();

Output

Before Text : [ ABCDE ]
After Text  : [ EDCBA ]

Before Text : [ 654A321 ]
After Text  : [ 123A456 ]

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved