right rotate a string

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

showing results for - "right rotate a string"
Riyad
10 May 2016
1import java.util.*;
2import java.util.*;
3public class Example114 {
4     public static void main(String[] arg) 
5   {
6	 String str = "abcdef"; 
7     char[] A = str.toCharArray();
8	 int offset=3;
9     int len = A.length;
10     offset %= len;
11     reverse(A, 0, len - offset - 1);
12     reverse(A, len - offset, len - 1);
13     reverse(A, 0, len - 1);
14     System.out.println("\n"+Arrays.toString(A));
15    }
16
17    private static void reverse(char[] str, int start, int end) {
18        while (start < end) {
19            char temp = str[start];
20            str[start] = str[end];
21            str[end] = temp;
22            start++;
23            end--;
24        }
25    }
26}
27
28