Reverse the string elements

Here given code implementation process.

// C++ program
// Reverse the string elements

//Include header file
#include <iostream>

using namespace std;
class MyString
{
	public:
		//This function which is reverse the given text string
		//Time complexity O(n)
		string reverse_str(string text)
		{
			string result = "";
			//Traverse the string element from end to front order
			for (int i = text.size() - 1; i >= 0; i--)
			{
				//append new character to end of result
				result += text[i];
			}
			//When string are not empty then
			//Returns reversal form of given text
			return result;
		}
};
int main()
{
	MyString obj = MyString();
	string text = "ssoB ehT mA I";
	cout << "\n Before : ";
	cout << text;
	text = obj.reverse_str(text);
	cout << "\n After : ";
	cout << text;
	//Case 2
	text = "edoc siht ekil I";
	cout << "\n Before : ";
	cout << text;
	text = obj.reverse_str(text);
	cout << "\n After : ";
	cout << text;
	return 0;
}

Output

 Before : ssoB ehT mA I
 After : I Am The Boss
 Before : edoc siht ekil I
 After : I like this code
// Java program
// Reverse the string elements
class MyString
{
	//This function which is reverse the given text string
	//Time complexity O(n)
	public String reverse_str(String text)
	{
		String result = "";
		//Traverse the string element from end to front order
		for (int i = text.length() - 1; i >= 0; i--)
		{
			//append new character to end of result
			result += text.charAt(i);
		}
		//When string are not empty then
		//Returns reversal form of given text
		return result;
	}
	public static void main(String[] args)
	{
		MyString obj = new MyString();
		String text = "ssoB ehT mA I";
		System.out.print("\n Before : ");
		System.out.print(text);
		text = obj.reverse_str(text);
		System.out.print("\n After : ");
		System.out.print(text);
		//Case 2
		text = "edoc siht ekil I";
		System.out.print("\n Before : ");
		System.out.print(text);
		text = obj.reverse_str(text);
		System.out.print("\n After : ");
		System.out.print(text);
	}
}

Output

 Before : ssoB ehT mA I
 After : I Am The Boss
 Before : edoc siht ekil I
 After : I like this code
//Include namespace system
using System;
// C# program
// Reverse the string elements
class MyString
{
	//This function which is reverse the given text string
	//Time complexity O(n)
	public String reverse_str(String text)
	{
		String result = "";
		//Traverse the string element from end to front order
		for (int i = text.Length - 1; i >= 0; i--)
		{
			//append new character to end of result
			result += text[i];
		}
		//When string are not empty then
		//Returns reversal form of given text
		return result;
	}
	public static void Main(String[] args)
	{
		MyString obj = new MyString();
		String text = "ssoB ehT mA I";
		Console.Write("\n Before : ");
		Console.Write(text);
		text = obj.reverse_str(text);
		Console.Write("\n After : ");
		Console.Write(text);
		//Case 2
		text = "edoc siht ekil I";
		Console.Write("\n Before : ");
		Console.Write(text);
		text = obj.reverse_str(text);
		Console.Write("\n After : ");
		Console.Write(text);
	}
}

Output

 Before : ssoB ehT mA I
 After : I Am The Boss
 Before : edoc siht ekil I
 After : I like this code
<?php
// Php program
// Reverse the string elements
class MyString
{
	//This function which is reverse the given text string
	//Time complexity O(n)
	public	function reverse_str($text)
	{
		$result = "";
		//Traverse the string element from end to front order
		for ($i = strlen($text) - 1; $i >= 0; $i--)
		{
			//append new character to end of result
			$result .= $text[$i];
		}
		//When string are not empty then
		//Returns reversal form of given text
		return $result;
	}
}

function main()
{
	$obj = new MyString();
	$text = "ssoB ehT mA I";
	echo "\n Before : ";
	echo $text;
	$text = $obj->reverse_str($text);
	echo "\n After : ";
	echo $text;
	//Case 2
	$text = "edoc siht ekil I";
	echo "\n Before : ";
	echo $text;
	$text = $obj->reverse_str($text);
	echo "\n After : ";
	echo $text;
}
main();

Output

 Before : ssoB ehT mA I
 After : I Am The Boss
 Before : edoc siht ekil I
 After : I like this code
// Node Js program
// Reverse the string elements
class MyString
{
	//This function which is reverse the given text string
	//Time complexity O(n)
	reverse_str(text)
	{
		var result = "";
		//Traverse the string element from end to front order
		for (var i = text.length - 1; i >= 0; i--)
		{
			//append new character to end of result
			result += text[i];
		}
		//When string are not empty then
		//Returns reversal form of given text
		return result;
	}
}

function main()
{
	var obj = new MyString();
	var text = "ssoB ehT mA I";
	process.stdout.write("\n Before : ");
	process.stdout.write(text);
	text = obj.reverse_str(text);
	process.stdout.write("\n After : ");
	process.stdout.write(text);
	//Case 2
	text = "edoc siht ekil I";
	process.stdout.write("\n Before : ");
	process.stdout.write(text);
	text = obj.reverse_str(text);
	process.stdout.write("\n After : ");
	process.stdout.write(text);
}
main();

Output

 Before : ssoB ehT mA I
 After : I Am The Boss
 Before : edoc siht ekil I
 After : I like this code
#  Python 3 program
#  Reverse the string elements
class MyString :
	# This function which is reverse the given text string
	# Time complexity O(n)
	def reverse_str(self, text) :
		result = ""
		i = len(text) - 1
		# Traverse the string element from end to front order
		while (i >= 0) :
			# append new character to end of result
			result += text[i]
			i -= 1
		
		# When string are not empty then
		# Returns reversal form of given text
		return result
	

def main() :
	obj = MyString()
	text = "ssoB ehT mA I"
	print("\n Before : ", end = "")
	print(text, end = "")
	text = obj.reverse_str(text)
	print("\n After : ", end = "")
	print(text, end = "")
	# Case 2
	text = "edoc siht ekil I"
	print("\n Before : ", end = "")
	print(text, end = "")
	text = obj.reverse_str(text)
	print("\n After : ", end = "")
	print(text, end = "")

if __name__ == "__main__": main()

Output

 Before : ssoB ehT mA I
 After : I Am The Boss
 Before : edoc siht ekil I
 After : I like this code
#  Ruby program
#  Reverse the string elements
class MyString

	# This function which is reverse the given text string
	# Time complexity O(n)
	def reverse_str(text)
	
		result = ""
		i = text.length() - 1
		# Traverse the string element from end to front order
		while (i >= 0)
		
			# append new character to end of result
			result += text[i]
			i -= 1
		end
		# When string are not empty then
		# Returns reversal form of given text
		return result
	end
end
def main()

	obj = MyString.new()
	text = "ssoB ehT mA I"
	print("\n Before : ")
	print(text)
	text = obj.reverse_str(text)
	print("\n After : ")
	print(text)
	# Case 2
	text = "edoc siht ekil I"
	print("\n Before : ")
	print(text)
	text = obj.reverse_str(text)
	print("\n After : ")
	print(text)
end
main()

Output

 Before : ssoB ehT mA I
 After : I Am The Boss
 Before : edoc siht ekil I
 After : I like this code
// Scala program
// Reverse the string elements
class MyString
{
	//This function which is reverse the given text string
	//Time complexity O(n)
	def reverse_str(text: String): String = {
		var result: String = "";
		var i: Int = text.length() - 1;
		//Traverse the string element from end to front order
		while (i >= 0)
		{
			//append new character to end of result
			result += text(i);
			i -= 1;
		}
		//When string are not empty then
		//Returns reversal form of given text
		return result;
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyString = new MyString();
		var text: String = "ssoB ehT mA I";
		print("\n Before : ");
		print(text);
		text = obj.reverse_str(text);
		print("\n After : ");
		print(text);
		//Case 2
		text = "edoc siht ekil I";
		print("\n Before : ");
		print(text);
		text = obj.reverse_str(text);
		print("\n After : ");
		print(text);
	}
}

Output

 Before : ssoB ehT mA I
 After : I Am The Boss
 Before : edoc siht ekil I
 After : I like this code
// Swift program
// Reverse the string elements
class MyString
{
	//This function which is reverse the given text string
	//Time complexity O(n)
	func reverse_str(_ data: String) -> String
	{
		var result: String = "";
      	var text = Array(data);
		var i: Int = text.count - 1;
		//Traverse the string element from end to front order
		while (i >= 0)
		{
			//append new character to end of result
			result += String(text[i]);
			i -= 1;
		}
		//When string are not empty then
		//Returns reversal form of given text
		return result;
	}
}
func main()
{
	let obj: MyString = MyString();
	var text: String = "ssoB ehT mA I";
	print("\n Before : ", terminator: "");
	print(text, terminator: "");
	text = obj.reverse_str(text);
	print("\n After : ", terminator: "");
	print(text, terminator: "");
	//Case 2
	text = "edoc siht ekil I";
	print("\n Before : ", terminator: "");
	print(text, terminator: "");
	text = obj.reverse_str(text);
	print("\n After : ", terminator: "");
	print(text, terminator: "");
}
main();

Output

 Before : ssoB ehT mA I
 After : I Am The Boss
 Before : edoc siht ekil I
 After : I like this code


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