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.
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.
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
The length L of the string S is from 3 to 100.
0 <= N <= L
Example Input/Output 1:
Input:
cricket
2
Input:
cricket
2
Output:
etcrick
etcrick
Example Input/Output 2:
Input:
truth
5
Input:
truth
5
Output:
truth
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!
//RMK
ReplyDeleteimport java.util.*;
public class Hello {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input = sc.next();
int n = sc.nextInt();
String left = input.substring(0,input.length()-n);
String right = input.substring(input.length()-n);
System.out.println(right+left);
}
}