Posted on by Kalkicode
Code String

Left and right rotation of string

Here given code implementation process.

// Java program
// Left and right rotation of string
class MyString
{
	//Perform left rotation of given string
	public String left_rotation(String str, int size)
	{
		//This variables are store the resultant information
		String left = "", right = "";
		//Loop which is iterate given string character elements
		for (int j = 0; j < str.length(); j++)
		{
			if (j < size)
			{
				left = left + str.charAt(j);
			}
			else
			{
				right = right + str.charAt(j);
			}
		}
		//Combine and return result
		return right + left;
	}
	//Perform right rotation of given string
	public String right_rotation(String str, int size)
	{
		//This variables are store the resultant information
		String left = "", right = "";
		//Loop which is iterate given string character elements
		for (int j = 0; j < str.length(); j++)
		{
			//Get initial n character
			if (j < str.length() - size)
			{
				left = left + str.charAt(j);
			}
			else
			{
				right = right + str.charAt(j);
			}
		}
		//Combine and return result
		return right + left;
	}
	//This function are handling the request of string rotation
	public void rotate_string(String str_data, int size)
	{
		if (size <= 0 || size > str_data.length())
		{
			//When rotation size is not valid
			return;
		}
		//Display given string
		System.out.print("\nGiven String  : [" + str_data + "]\n");
		System.out.print("Rotation size " + size);
		String result = left_rotation(str_data, size);
		//Show left rotation of given size
		System.out.print("\nLeft Rotation  : " + result);
		result = right_rotation(str_data, size);
		//Show right rotation of given size
		//When need to use rotation result simply return its value
		System.out.print("\nRight Rotation : " + result + "\n");
	}
	public static void main(String[] args)
	{
		MyString obj = new MyString();
		// Simple case
		obj.rotate_string("ABCDEFGHIJ", 3);
		obj.rotate_string("1234567890", 6);
	}
}

Output

Given String  : [ABCDEFGHIJ]
Rotation size 3
Left Rotation  : DEFGHIJABC
Right Rotation : HIJABCDEFG

Given String  : [1234567890]
Rotation size 6
Left Rotation  : 7890123456
Right Rotation : 5678901234
// C++ program
// Left and right rotation of string

//Include header file
#include <iostream>
using namespace std;

class MyString
{
	public:
		//Perform left rotation of given string
		string left_rotation(string str, int size)
		{
			//This variables are store the resultant information
			string left = "", right = "";
			//Loop which is iterate given string character elements
			for (int j = 0; j < str.size(); j++)
			{
				if (j < size)
				{
					left = left + str[j];
				}
				else
				{
					right = right + str[j];
				}
			}
			//Combine and return result
			return right + left;
		}
	//Perform right rotation of given string
	string right_rotation(string str, int size)
	{
		//This variables are store the resultant information
		string left = "", right = "";
		//Loop which is iterate given string character elements
		for (int j = 0; j < str.size(); j++)
		{
			//Get initial n character
			if (j < str.size() - size)
			{
				left = left + str[j];
			}
			else
			{
				right = right + str[j];
			}
		}
		//Combine and return result
		return right + left;
	}
	//This function are handling the request of string rotation
	void rotate_string(string str_data, int size)
	{
		if (size <= 0 || size > str_data.size())
		{
			//When rotation size is not valid
			return;
		}
		//Display given string
		cout << "\nGiven String  : [" << str_data << "]\n";
		cout << "Rotation size " << size;
		string result = left_rotation(str_data, size);
		//Show left rotation of given size
		cout << "\nLeft Rotation  : " << result;
		result = right_rotation(str_data, size);
		//Show right rotation of given size
		//When need to use rotation result simply return its value
		cout << "\nRight Rotation : " << result << "\n";
	}
};
int main()
{
	MyString obj = MyString();
	// Simple case
	obj.rotate_string("ABCDEFGHIJ", 3);
	obj.rotate_string("1234567890", 6);
	return 0;
}

Output

Given String  : [ABCDEFGHIJ]
Rotation size 3
Left Rotation  : DEFGHIJABC
Right Rotation : HIJABCDEFG

Given String  : [1234567890]
Rotation size 6
Left Rotation  : 7890123456
Right Rotation : 5678901234
// C# program
// Left and right rotation of string

//Include namespace system
using System;
class MyString
{
	//Perform left rotation of given string
	public String left_rotation(String str, int size)
	{
		//This variables are store the resultant information
		String left = "", right = "";
		//Loop which is iterate given string character elements
		for (int j = 0; j < str.Length; j++)
		{
			if (j < size)
			{
				left = left + str[j];
			}
			else
			{
				right = right + str[j];
			}
		}
		//Combine and return result
		return right + left;
	}
	//Perform right rotation of given string
	public String right_rotation(String str, int size)
	{
		//This variables are store the resultant information
		String left = "", right = "";
		//Loop which is iterate given string character elements
		for (int j = 0; j < str.Length; j++)
		{
			//Get initial n character
			if (j < str.Length - size)
			{
				left = left + str[j];
			}
			else
			{
				right = right + str[j];
			}
		}
		//Combine and return result
		return right + left;
	}
	//This function are handling the request of string rotation
	public void rotate_string(String str_data, int size)
	{
		if (size <= 0 || size > str_data.Length)
		{
			//When rotation size is not valid
			return;
		}
		//Display given string
		Console.Write("\nGiven String  : [" + str_data + "]\n");
		Console.Write("Rotation size " + size);
		String result = left_rotation(str_data, size);
		//Show left rotation of given size
		Console.Write("\nLeft Rotation  : " + result);
		result = right_rotation(str_data, size);
		//Show right rotation of given size
		//When need to use rotation result simply return its value
		Console.Write("\nRight Rotation : " + result + "\n");
	}
	public static void Main(String[] args)
	{
		MyString obj = new MyString();
		// Simple case
		obj.rotate_string("ABCDEFGHIJ", 3);
		obj.rotate_string("1234567890", 6);
	}
}

Output

Given String  : [ABCDEFGHIJ]
Rotation size 3
Left Rotation  : DEFGHIJABC
Right Rotation : HIJABCDEFG

Given String  : [1234567890]
Rotation size 6
Left Rotation  : 7890123456
Right Rotation : 5678901234
<?php
// Php program
// Left and right rotation of string
class MyString
{
	//Perform left rotation of given string
	public	function left_rotation($str, $size)
	{
		//This variables are store the resultant information
		$left = "";
		$right = "";
		//Loop which is iterate given string character elements
		for ($j = 0; $j < strlen($str); $j++)
		{
			if ($j < $size)
			{
				$left = $left . $str[$j];
			}
			else
			{
				$right = $right . $str[$j];
			}
		}
		//Combine and return result
		return $right . $left;
	}
	//Perform right rotation of given string
	public	function right_rotation($str, $size)
	{
		//This variables are store the resultant information
		$left = "";
		$right = "";
		//Loop which is iterate given string character elements
		for ($j = 0; $j < strlen($str); $j++)
		{
			//Get initial n character
			if ($j < strlen($str) - $size)
			{
				$left = $left . $str[$j];
			}
			else
			{
				$right = $right . $str[$j];
			}
		}
		//Combine and return result
		return $right . $left;
	}
	//This function are handling the request of string rotation
	public	function rotate_string($str_data, $size)
	{
		if ($size <= 0 || $size > strlen($str_data))
		{
			//When rotation size is not valid
			return;
		}
		echo "\nGiven String  : [". $str_data ."]\n";
		echo "Rotation size ". $size;
		$result = $this->left_rotation($str_data, $size);
		echo "\nLeft Rotation  : ". $result;
		$result = $this->right_rotation($str_data, $size);
		echo "\nRight Rotation : ". $result ."\n";
	}
}

function main()
{
	$obj = new MyString();
	// Simple case
	$obj->rotate_string("ABCDEFGHIJ", 3);
	$obj->rotate_string("1234567890", 6);
}
main();

Output

Given String  : [ABCDEFGHIJ]
Rotation size 3
Left Rotation  : DEFGHIJABC
Right Rotation : HIJABCDEFG

Given String  : [1234567890]
Rotation size 6
Left Rotation  : 7890123456
Right Rotation : 5678901234
// Node Js program
// Left and right rotation of string
class MyString
{
	//Perform left rotation of given string
	left_rotation(str, size)
	{
		//This variables are store the resultant information
		var left = "";
		var right = "";
		//Loop which is iterate given string character elements
		for (var j = 0; j < str.length; j++)
		{
			if (j < size)
			{
				left = left + str[j];
			}
			else
			{
				right = right + str[j];
			}
		}
		//Combine and return result
		return right + left;
	}
	//Perform right rotation of given string
	right_rotation(str, size)
	{
		//This variables are store the resultant information
		var left = "";
		var right = "";
		//Loop which is iterate given string character elements
		for (var j = 0; j < str.length; j++)
		{
			//Get initial n character
			if (j < str.length - size)
			{
				left = left + str[j];
			}
			else
			{
				right = right + str[j];
			}
		}
		//Combine and return result
		return right + left;
	}
	//This function are handling the request of string rotation
	rotate_string(str_data, size)
	{
		if (size <= 0 || size > str_data.length)
		{
			//When rotation size is not valid
			return;
		}
		process.stdout.write("\nGiven String  : [" + str_data + "]\n");
		process.stdout.write("Rotation size " + size);
		var result = this.left_rotation(str_data, size);
		process.stdout.write("\nLeft Rotation  : " + result);
		result = this.right_rotation(str_data, size);
		process.stdout.write("\nRight Rotation : " + result + "\n");
	}
}

function main()
{
	var obj = new MyString();
	// Simple case
	obj.rotate_string("ABCDEFGHIJ", 3);
	obj.rotate_string("1234567890", 6);
}
main();

Output

Given String  : [ABCDEFGHIJ]
Rotation size 3
Left Rotation  : DEFGHIJABC
Right Rotation : HIJABCDEFG

Given String  : [1234567890]
Rotation size 6
Left Rotation  : 7890123456
Right Rotation : 5678901234
#  Python 3 program
#  Left and right rotation of string
class MyString :
	# Perform left rotation of given string
	def left_rotation(self, str, size) :
		# This variables are store the resultant information
		left = ""
		right = ""
		j = 0
		# Loop which is iterate given string character elements
		while (j < len(str)) :
			if (j < size) :
				left = left + str[j]
			else :
				right = right + str[j]
			
			j += 1
		
		# Combine and return result
		return right + left
	
	# Perform right rotation of given string
	def right_rotation(self, str, size) :
		# This variables are store the resultant information
		left = ""
		right = ""
		# Loop which is iterate given string character elements
		j = 0
		while (j < len(str)) :
			# Get initial n character
			if (j < len(str) - size) :
				left = left + str[j]
			else :
				right = right + str[j]
			
			j += 1
		
		# Combine and return result
		return right + left
	
	# This function are handling the request of string rotation
	def rotate_string(self, str_data, size) :
		if (size <= 0 or size > len(str_data)) :
			# When rotation size is not valid
			return
		
		print("\nGiven String  : [{0}]".format(str_data))
		print("Rotation size ", size)
		result = self.left_rotation(str_data, size)
		print("Left Rotation  : [{0}]".format(result))
		result = self.right_rotation(str_data, size)
		print("Right Rotation : [{0}]".format(result))
	

def main() :
	obj = MyString()
	#  Simple case
	obj.rotate_string("ABCDEFGHIJ", 3)
	obj.rotate_string("1234567890", 6)

if __name__ == "__main__": main()

Output

Given String  : [ABCDEFGHIJ]
Rotation size  3
Left Rotation  : [DEFGHIJABC]
Right Rotation : [HIJABCDEFG]

Given String  : [1234567890]
Rotation size  6
Left Rotation  : [7890123456]
Right Rotation : [5678901234]
#  Ruby program
#  Left and right rotation of string
class MyString

	# Perform left rotation of given string
	def left_rotation(str, size)
	
		# This variables are store the resultant information
		left = ""
		right = ""
		j = 0
		# Loop which is iterate given string character elements
		while (j < str.length())
		
			if (j < size)
			
				left = left + str[j]
			else
			
				right = right + str[j]
			end
			j += 1
		end
		# Combine and return result
		return right + left
	end
	# Perform right rotation of given string
	def right_rotation(str, size)
	
		# This variables are store the resultant information
		left = ""
		right = ""
		# Loop which is iterate given string character elements
		j = 0
		while (j < str.length())
		
			# Get initial n character
			if (j < str.length() - size)
			
				left = left + str[j]
			else
			
				right = right + str[j]
			end
			j += 1
		end
		# Combine and return result
		return right + left
	end
	# This function are handling the request of string rotation
	def rotate_string(str_data, size)
	
		if (size <= 0 || size > str_data.length())
		
			# When rotation size is not valid
			return
		end
		# Display given string
		print("\nGiven String  : [", str_data ,"]\n")
		print("Rotation size ", size)
		result = self.left_rotation(str_data, size)
		# Show left rotation of given size
		print("\nLeft Rotation  : ", result)
		result = self.right_rotation(str_data, size)
		# Show right rotation of given size
		# When need to use rotation result simply return its value
		print("\nRight Rotation : ", result ,"\n")
	end
end
def main()

	obj = MyString.new()
	#  Simple case
	obj.rotate_string("ABCDEFGHIJ", 3)
	obj.rotate_string("1234567890", 6)
end
main()

Output

Given String  : [ABCDEFGHIJ]
Rotation size 3
Left Rotation  : DEFGHIJABC
Right Rotation : HIJABCDEFG

Given String  : [1234567890]
Rotation size 6
Left Rotation  : 7890123456
Right Rotation : 5678901234
// Scala program
// Left and right rotation of string
class MyString
{
	//Perform left rotation of given string
	def left_rotation(str: String, size: Int): String = {
		//This variables are store the resultant information
		var left: String = "";
		var right: String = "";
		var j: Int = 0;
		//Loop which is iterate given string character elements
		while (j < str.length())
		{
			if (j < size)
			{
				left = left + str(j);
			}
			else
			{
				right = right + str(j);
			}
			j += 1;
		}
		//Combine and return result
		return right + left;
	}
	//Perform right rotation of given string
	def right_rotation(str: String, size: Int): String = {
		//This variables are store the resultant information
		var left: String = "";
		var right: String = "";
		//Loop which is iterate given string character elements
		var j: Int = 0;
		while (j < str.length())
		{
			//Get initial n character
			if (j < str.length() - size)
			{
				left = left + str(j);
			}
			else
			{
				right = right + str(j);
			}
			j += 1;
		}
		//Combine and return result
		return right + left;
	}
	//This function are handling the request of string rotation
	def rotate_string(str_data: String, size: Int): Unit = {
		if (size <= 0 || size > str_data.length())
		{
			//When rotation size is not valid
			return;
		}
		//Display given string
		print("\nGiven String  : [" + str_data + "]\n");
		print("Rotation size " + size);
		var result: String = left_rotation(str_data, size);
		//Show left rotation of given size
		print("\nLeft Rotation  : " + result);
		result = right_rotation(str_data, size);
		//Show right rotation of given size
		//When need to use rotation result simply return its value
		print("\nRight Rotation : " + result + "\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyString = new MyString();
		// Simple case
		obj.rotate_string("ABCDEFGHIJ", 3);
		obj.rotate_string("1234567890", 6);
	}
}

Output

Given String  : [ABCDEFGHIJ]
Rotation size 3
Left Rotation  : DEFGHIJABC
Right Rotation : HIJABCDEFG

Given String  : [1234567890]
Rotation size 6
Left Rotation  : 7890123456
Right Rotation : 5678901234
// Swift program
// Left and right rotation of string
class MyString
{
	//Perform left rotation of given string
	func left_rotation(_ text: String, _ size: Int) -> String
	{
		//This variables are store the resultant information
		var left: String = "";
		var right: String = "";
		var j: Int = 0;
      	let str = Array(text);
		//Loop which is iterate given string character elements
		while (j < str.count)
		{
			if (j < size)
			{
				left = left + String(str[j]);
			}
			else
			{
				right = right + String(str[j]);
			}
			j += 1;
		}
		//Combine and return result
		return right + left;
	}
	//Perform right rotation of given string
	func right_rotation(_ text: String, _ size: Int) -> String
	{
		//This variables are store the resultant information
		var left: String = "";
		var right: String = "";
      	let str = Array(text);
		//Loop which is iterate given string character elements
		var j: Int = 0;
		while (j < str.count)
		{
			//Get initial n character
			if (j < str.count - size)
			{
				left = left + String(str[j]);
			}
			else
			{
				right = right + String(str[j]);
			}
			j += 1;
		}
		//Combine and return result
		return right + left;
	}
	//This function are handling the request of string rotation
	func rotate_string(_ str_data: String, _ size: Int)
	{
		if (size <= 0 || size > str_data.count)
		{
			//When rotation size is not valid
			return;
		}
		print("\nGiven String  : [", str_data ,"]\n", terminator: "");
		print("Rotation size ", size, terminator: "");
		var result: String = self.left_rotation(str_data, size);
		print("\nLeft Rotation  : ", result, terminator: "");
		result = self.right_rotation(str_data, size);
		print("\nRight Rotation : ", result ,"\n", terminator: "");
	}
}
func main()
{
	let obj: MyString = MyString();
	// Simple case
	obj.rotate_string("ABCDEFGHIJ", 3);
	obj.rotate_string("1234567890", 6);
}
main();

Output

Given String  : [ ABCDEFGHIJ ]
Rotation size  3
Left Rotation  :  DEFGHIJABC
Right Rotation :  HIJABCDEFG

Given String  : [ 1234567890 ]
Rotation size  6
Left Rotation  :  7890123456
Right Rotation :  5678901234

Comment

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