Shifting characters within a string
Solution 1
newStr = newStr.charAt(newStr.length() - 1) + newStr.substring(0, newStr.length() - 1);
Solution 2
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 :
wStackoverflo
owStackoverfl
lowStackoverf
flowStackover
rflowStackove
erflowStackov
verflowStacko
overflowStack
koverflowStac
ckoverflowSta
ackoverflowSt
tackoverflowS
Stackoverflow
theGreenCabbage
A software developer and supporter of any language that facilitates open source methodologies, extensibility, versatility and collaboration. Currently Using: PHP, MySQL, and bash. Current Projects: OpenCart module development. I am also the only living programmer that's also a part time leafy green veggie. Yeah. I'm that good. I make you green with envy. Bachelor of Science in Computer Engineering Minor in Computer Science Grasshopper 3D developer KT Tools. A Grasshopper 3D tool suite I authored Make Eclipse Look and Work More Like Visual Studio
Updated on July 09, 2022Comments
-
theGreenCabbage almost 2 years
String newStr; public RandomCuriosity(String input){ newStr = input; } public void shiftChars(){ char[] oldChar = newStr.toCharArray(); char[] newChar = new char[oldChar.length]; newChar[0] = oldChar[oldChar.length-1]; for(int i = 1; i < oldChar.length; i++){ newChar[i] = oldChar[i-1]; } newStr = String.valueOf(newChar); }
I created a method that shifts characters forward by one. For example, the input could be:
The input:
Stackoverflow
The output:
wStackoverflo
How I did it is I mutated an instance of a string. Convert that string to a
char
array (calling itoldChar
), assigned the last index of ofoldChar
as the first index ofnewChar
, and made a for-loop that took the first index ofoldChar
as the second index of my newChar
array and so forth. Lastly, I converted the char array back to a string.I feel like I did way too much to do something very simple. Is there a more efficient way to do something like this?
EDIT Thanks for the great answers!
-
theGreenCabbage over 10 yearsWoah. That's an intense one-liner. So let me get this straight. You treated the last index as a char, and the chars before the last index as a complete string?
-
JB Nizet over 10 yearsYes. But you may also use
newStr.substring(newStr.length() - 1)
as the first operand if you find it clearer. -
theGreenCabbage over 10 yearsThis is a very beautiful solution, and a great use of in-built Java methods. Thanks JB.
-
Sotirios Delimanolis over 10 years@JB Can you check my last comment to the question and comment on if I am right about the complexity?
-
JB Nizet over 10 years@SotiriosDelimanolis: looks right to me. O(3n) is O(n) though. Anyway, unless the string is really huge, the performance of such a simple task should not be something you would try to optimize.
-
theGreenCabbage over 10 years@JBNizet Out of curiosity, is there any thing inherently bad about my original code?
-
JB Nizet over 10 years@theGreenCabbage: there's nothing inherently bad, but it's longer and harder to understand than the one-liner that uses higher-level methods. The one-liner does exactly what you intend to do: create a string beginning with the last char and ending with the n-1 first chars. Without javadoc, your implementation requires a deeper analysis to understand what it does. Using frequently used library methods like substring also guarantees that the code will be jitted and benefit from any optimization that might be introduced in the JDK.
-
Caco almost 7 yearsJust correcting code line. It is missing
()
in secondnewStr.length
. Would be:newStr = newStr.charAt(newStr.length() - 1) + newStr.substring(0, newStr.length() - 1);
-
Scratte about 4 yearsYour solution is the same as the accepted answer by JB Nizet.
-
Omar Abusabha about 4 yearsnot matter a lot. Actually, just I want to share how I used this code with my problem, thank you to get your attention on the code ^_^
-
Scratte about 4 yearsYour Answer may be deleted though. The idea is to post different answers.