public class StringRotation { public static String rotateString(String str, int rotation) { if (str == null || str.isEmpty() || rotation == 0) { return str; } int length = str.length(); rotation = rotation % length; // To handle rotations larger than the string length if (rotation < 0) { rotation += length; // Convert negative rotation to positive equivalent } // Divide and concatenate parts of the string String rotated = str.substring(rotation) + str.substring(0, rotation); return rotated; } public static void main(String[] args) { String str = "hello"; int rotation = 2; // Positive rotates left, negative rotates right String result = rotateString(str, rotation); System.out.println("Original String: " + str); System.out.println("Rotated String: " + result); } }