Rotate String – N Positions

                    Rotate String - N Positions


A string S of length L is passed as the input. The program must rotate the string S by N position in forward direction and print the result as the output.
Input Format:
The first line will contain the value of S.
The second line will contain N.
Output Format:
The first line will contain the rotated string value.
Boundary Conditions:
The length L of the string S is from 3 to 100.
0 <= N <= L
Example Input/Output 1:
Input:
cricket
2
Output:
etcrick
Example Input/Output 2:
Input:
truth
5
Output:
truth

Code:

import java.util.*;
public class Hello {
    public static void main(String[] args) {
                   Scanner sc=new Scanner(System.in);
          String str=sc.next();
          int n=sc.nextInt();
          char[] ch=str.toCharArray();
          List<Character> li=new ArrayList<>();
    for(Character c:ch)
        {
            li.add(c);
        }
    Collections.rotate(li,n);
    for(Character c:li)
        System.out.print(c);
          }
}



Please do comment If u have any Queries!

One Comment

Add a Comment

Your email address will not be published. Required fields are marked *