Answers for "rotate string in java"

0

rotate string in java

// Rotate a string both left and right

import java.util.*;
import java.io.*;
 
public class Test {

    // rotate s left by d positions
    static String leftrotate(String str, int d) {
            String ans = str.substring(d) + str.substring(0, d);
            return ans;
    }
 
    // rotates s right by d positions
    static String rightrotate(String str, int d) {
            return leftrotate(str, str.length() - d);
    }
 
    public static void main(String args[]) {
            String str1 = "The quick brown fox jumped over the lazy dog.";
            System.out.println(leftrotate(str1, 4));
 
            String str2 = "The quick brown fox jumped over the lazy dog.";
            System.out.println(rightrotate(str2, 4));
    }
}
Posted by: Guest on October-05-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language