Right Rotate String

This program performs right rotation of a string.

Problem Statement

Write a Java program to right rotate string "Eduinq" by 2 positions.

Source Code

1 public class RightRotateString {
2 public static void main(String[] args) {
3 System.out.println("Eduinq Right Rotate String");
4 String str="Eduinq";
5 int d=2;
6 String rotated=str.substring(str.length()-d)+str.substring(0,str.length()-d);
7 System.out.println("Right rotated string: "+rotated);
8 }
9 }

Program Output

Eduinq Right Rotate String
Right rotated string: nqEdui

Explanation

The right rotation takes the last d characters of the string and moves them to the front, achieved by combining two substring calls, demonstrating right rotation of a string.

Quick Links to Explore