rotate string in java

Solutions on MaxInterview for rotate string in java by the best coders in the world

showing results for - "rotate string in java"
Martina
14 Jun 2016
1// Rotate a string both left and right
2
3import java.util.*;
4import java.io.*;
5 
6public class Test {
7
8    // rotate s left by d positions
9    static String leftrotate(String str, int d) {
10            String ans = str.substring(d) + str.substring(0, d);
11            return ans;
12    }
13 
14    // rotates s right by d positions
15    static String rightrotate(String str, int d) {
16            return leftrotate(str, str.length() - d);
17    }
18 
19    public static void main(String args[]) {
20            String str1 = "The quick brown fox jumped over the lazy dog.";
21            System.out.println(leftrotate(str1, 4));
22 
23            String str2 = "The quick brown fox jumped over the lazy dog.";
24            System.out.println(rightrotate(str2, 4));
25    }
26}