Rotate String in Right Direction in java
Rotate String in Right Direction in java:
Program:
import java.util.*;
public class RotateString {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
char[] ch = new char[n];
for(int i=0;i<n;i++) {
ch[i] = s.next().charAt(0);
}
int r = s.nextInt();
char temp = 'a';
r=r%n;
while(r>0) {
temp = ch[n-1];
for(int i = n-1; i>0;i--) {
ch[i] = ch[i-1];
}
ch[0] = temp;
System.out.println("r: " + r);
for(int i=0;i<n;i++) {
System.out.print(ch[i]);
}
System.out.println();
r--;
}
for(int i=0;i<n;i++) {
System.out.print(ch[i]);
}
}
}
Output:
5
a
s
d
f
g
3
r: 3
gasdf
r: 2
fgasd
r: 1
dfgas
dfgas
Happy Coding 💻:-)
Comments
Post a Comment