Answers for "right rotate a string"

0

right rotate a string

import java.util.*;
import java.util.*;
public class Example114 {
     public static void main(String[] arg) 
   {
	 String str = "abcdef"; 
     char[] A = str.toCharArray();
	 int offset=3;
     int len = A.length;
     offset %= len;
     reverse(A, 0, len - offset - 1);
     reverse(A, len - offset, len - 1);
     reverse(A, 0, len - 1);
     System.out.println("\n"+Arrays.toString(A));
    }

    private static void reverse(char[] str, int start, int end) {
        while (start < end) {
            char temp = str[start];
            str[start] = str[end];
            str[end] = temp;
            start++;
            end--;
        }
    }
}
Posted by: Guest on August-20-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language