package Sample; /*Given a number x and a sentence, print the sentence rotated x times clockwise if x is positive and anti-clockwise otherwise. Note:The sentence contains no white spaces*/ /*SAMPLE INPUT 5 Softathlon SAMPLE OUTPUT thlonSofta*/ --------------------------------------------------------------------------------------------------------------------- public class blog1 { public static void main(String[] args) { int n=-5; String g ="Softathlon"; if(n>0) System.out.println(clockWise(n, g)); else System.out.println(antiClockWise((n*-1), g)); } public static String clockWise(int n, String g) { String outString=""; String s1 = g.substring(0, n); String s2 = g.substring(n, g.length()); outString=s2+s1; return outString; } public static String antiClockWise(int n, String g) { String outString=""; String s1 = g.substring(0, (g.length()-n)); String temp = g.substring((g.length()-n), g.length()); String out=""; for (int i =temp.length()-1; i >=0; i--) { out +=temp.charAt(i); } outString=out+s1; return outString; } }
Comments
Post a Comment