print all rotations in string

Here given code implementation process.

//C Program 
//Display String rotation
#include <stdio.h>


void print_rotation(char str[],int size,int start)
{
  
  for (int i = 0; i < size; ++i)
  {
    if(start >= size)
    {
      start=0;
    }
    printf("%c",str[start] );
    
    start++;
  }
  printf("\n");
}

void rotations(char str[],int size)
{ 
  printf("\nString : %s\n",str );

  for (int i = 0; i < size; ++i)
  {
    print_rotation(str,size,i);
  }
 
}
int main()
{


  char str1[] = "Winner";

  int s1=sizeof(str1)/sizeof(str1[0])-1;

  rotations(str1,s1);
  

  char str2[] = "Test";

  s1=sizeof(str2)/sizeof(str2[0])-1;

  rotations(str2,s1);
  
  return 0;
}

Output

String : Winner
Winner
innerW
nnerWi
nerWin
erWinn
rWinne

String : Test
Test
estT
stTe
tTes
// Java program
// Display String rotation

public class MyString {
  
  public void print_rotation(String str, int size, int start) 
  {
    for (int i = 0; i < size; ++i) 
    {
      if (start >= size) 
      {
        start = 0;
      }
      System.out.print(str.charAt(start));
      start++;
    }
    System.out.print("\n");
  }
  public void rotations(String str) 
  {
    //Get the size of given string
    int size = str.length();

    System.out.print("\nString : "+str+"\n" );

    for (int i = 0; i < size; ++i) 
    {
      print_rotation(str, size, i);
    }
  }
  public static void main(String[] args) 
  {

    MyString obj = new MyString();
    
    obj.rotations("Winner");
    obj.rotations("Test");
  
  }
}

Output

String : Winner
Winner
innerW
nnerWi
nerWin
erWinn
rWinne

String : Test
Test
estT
stTe
tTes
// C++ program
// Display String rotation
#include<iostream>

using namespace std;
class MyString {
	public:
    void print_rotation(string str, int size, int start) {
      for (int i = 0; i < size; ++i) {
        if (start >= size) {
          start = 0;
        }
        cout << str[start];
        start++;
      }
      cout << "\n";
    }
	void rotations(string str) {
		//Get the size of given string
		int size = str.size();
		cout << "\nString : " << str << "\n";
		for (int i = 0; i < size; ++i) {
			this->print_rotation(str, size, i);
		}
	}
};
int main() {
	MyString obj =  MyString();
	obj.rotations("Winner");
	obj.rotations("Test");
	return 0;
}

Output

String : Winner
Winner
innerW
nnerWi
nerWin
erWinn
rWinne

String : Test
Test
estT
stTe
tTes
// C# program
// Display String rotation
using System;
public class MyString {
	public void print_rotation(String str, int size, int start) {
		for (int i = 0; i < size; ++i) {
			if (start >= size) {
				start = 0;
			}
			Console.Write(str[start]);
			start++;
		}
		Console.Write("\n");
	}
	public void rotations(String str) {
		//Get the size of given string
		int size = str.Length;
		Console.Write("\nString : " + str + "\n");
		for (int i = 0; i < size; ++i) {
			print_rotation(str, size, i);
		}
	}
	public static void Main(String[] args) {
		MyString obj = new MyString();
		obj.rotations("Winner");
		obj.rotations("Test");
	}
}

Output

String : Winner
Winner
innerW
nnerWi
nerWin
erWinn
rWinne

String : Test
Test
estT
stTe
tTes
<?php
// Php program
// Display String rotation
class MyString {
	public 	function print_rotation($str, $size, $start) {
		for ($i = 0; $i < $size; ++$i) {
			if ($start >= $size) {
				$start = 0;
			}
			echo($str[$start]);
			$start++;
		}
		echo("\n");
	}
	public 	function rotations($str) {
		//Get the size of given string
		$size = strlen($str);
		echo("\nString : ". $str ."\n");
		for ($i = 0; $i < $size; ++$i) {
			$this->print_rotation($str, $size, $i);
		}
	}
}

function main() {
	$obj = new MyString();
	$obj->rotations("Winner");
	$obj->rotations("Test");

}
main();

Output

String : Winner
Winner
innerW
nnerWi
nerWin
erWinn
rWinne

String : Test
Test
estT
stTe
tTes
// Node Js program
// Display String rotation
class MyString {
	print_rotation(str, size, start) {
		for (var i = 0; i < size; ++i) {
			if (start >= size) {
				start = 0;
			}

			process.stdout.write(str[start]);
			start++;
		}

		process.stdout.write("\n");
	}
	rotations(str) {
		//Get the size of given string
		var size = str.length;
		process.stdout.write("\nString : " + str + "\n");
		for (var i = 0; i < size; ++i) {
			this.print_rotation(str, size, i);
		}
	}
}

function main(args) {
	var obj = new MyString();
	obj.rotations("Winner");
	obj.rotations("Test");
}

main();

Output

String : Winner
Winner
innerW
nnerWi
nerWin
erWinn
rWinne

String : Test
Test
estT
stTe
tTes
#  Python 3 program
#  Display String rotation
class MyString :
	def print_rotation(self, str, size, start) :
		i = 0
		while (i < size) :
			if (start >= size) :
				start = 0
			
			print(str[start], end = "")
			start += 1
			i += 1
		
		print("\n", end = "")
	
	def rotations(self, str) :
		size = len(str)
		print("\nString : ", str ,"\n", end = "")
		i = 0
		while (i < size) :
			self.print_rotation(str, size, i)
			i += 1
		
	

def main() :
	obj = MyString()
	obj.rotations("Winner")
	obj.rotations("Test")


if __name__ == "__main__":
	main()

Output

String :  Winner
Winner
innerW
nnerWi
nerWin
erWinn
rWinne

String :  Test
Test
estT
stTe
tTes
#  Ruby program
#  Display String rotation
class MyString 
	def print_rotation(str, size, start) 
		i = 0
		while (i < size) 
			if (start >= size) 
				start = 0
			end
			print(str[start])
			start += 1
			i += 1
		end
		print("\n")
	end
	def rotations(str) 
		size = str.length()
		print("\nString  :", str ,"\n")
		i = 0
		while (i < size) 
			self.print_rotation(str, size, i)
			i += 1
		end
	end
end
def main() 
	obj = MyString.new()
	obj.rotations("Winner")
	obj.rotations("Test")
end
main()

Output

String  :Winner
Winner
innerW
nnerWi
nerWin
erWinn
rWinne

String  :Test
Test
estT
stTe
tTes
// Scala program
// Display String rotation
class MyString {
	def print_rotation(str: String, size: Int, s: Int): Unit = {
		var i: Int = 0;
      	var start : Int = s;
		while (i < size) {
			if (start >= size) {
				start = 0;
			}
			print(str(start));
			start += 1;
			i += 1;
		}
		print("\n");
	}
	def rotations(str: String): Unit = {
		var size: Int = str.length();
		print("\nString : " + str + "\n");
		var i: Int = 0;
		while (i < size) {
			print_rotation(str, size, i);
			i += 1;
		}
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var obj: MyString = new MyString();
		obj.rotations("Winner");
		obj.rotations("Test");
	}
}

Output

String : Winner
Winner
innerW
nnerWi
nerWin
erWinn
rWinne

String : Test
Test
estT
stTe
tTes
// Swift program
// Display String rotation
class MyString {
	func print_rotation(_ str: String, _ size: Int, _ s: Int) {
		var i: Int = 0;
      	var data = Array(str)
        var start = s;
		while (i < size) {
			if (start >= size) {
				start = 0;
			}
			print(data[start], terminator: "");
			start += 1;
			i += 1;
		}
		print("\n", terminator: "");
	}
	func rotations(_ str: String) {
		let size: Int = str.count;
		print("\nString : ", str );
		var i: Int = 0;
		while (i < size) {
			self.print_rotation(str, size, i);
			i += 1;
		}
	}
}
func main() {
	let obj: MyString = MyString();
	obj.rotations("Winner");
	obj.rotations("Test");
}
main();

Output

String :  Winner
Winner
innerW
nnerWi
nerWin
erWinn
rWinne

String :  Test
Test
estT
stTe
tTes


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