Shifting characters within a string Shifting characters within a string arrays arrays

Shifting characters within a string


newStr = newStr.charAt(newStr.length() - 1) + newStr.substring(0, newStr.length() - 1);


You can made your life simpler :

public static void main (String[] args) throws java.lang.Exception {    String input = "Stackoverflow";    for(int i = 0; i < s.length(); i++){        input = shift(input);        System.out.println(input);    }}public static String shift(String s) {    return s.charAt(s.length()-1)+s.substring(0, s.length()-1);}

Output :

wStackoverfloowStackoverfllowStackoverfflowStackoverrflowStackoveerflowStackovverflowStackooverflowStackkoverflowStacckoverflowStaackoverflowSttackoverflowSStackoverflow


You could use System.arrayCopy:

char[] oldChar = newStr.toCharArray();char[] newChar = new char[oldChar.length];newChar[0] = oldChar[oldChar.length - 1];System.arrayCopy(oldChar, 0, newChar, 1, oldChar.length - 1);