Write a method that given an input String and an integer, returns a new String that is each character of the input String shifted a number of places as indicated by the integer (positive int shifts right, negative int shifts left). The method should have the following signature: public String shiftString(String s, int n) For example, if s = "foobar" and n = 1, the method should return "rfooba". If n = -1, the method returns "oobarf". If n = 8, the mehod returns "arfoob". For more of a challenge, don't use the String functions other than maybe toCharArray(). [ June 27, 2003: Message edited by: Jason Menard ]
Anupam's first method is much, much faster than the second. It's possible to modify the second method to be much closer to the first in terms of efficiency - but so far I can't find anything that actually beats it. There's probably a useful lesson here about how it's usually best to use standard library functions wherever possible, and keep things simple. Turns out it's also most efficient.
This also seems to be working but haven't tested efficiency.
Joined: Nov 09, 2000
Joined: Jan 30, 2000
OK, here was my version:
I figure it toCharArray() was allowec, why not getChars()? That way there's no time wasted copying chars to the wrong position. Still, performance-wise we can't really beat or the equivalent The main advantage these have over any solution that builds a char array is that they can take advantage of the new String(StringBuffer) constructor, which is optimized so that it can build a String using the same internal char array which the StringBuffer was using. Which is very fast; no new char is created, and no copying needs to be done. In comparison, the new String(char) constructor is slower because it must make a copy of the char for internal use by the String. The reason for this is that String must be immutable; it can't just take a char from yyou and assume it will never change. It has to make its own private copy to ensure that it doesn't change. So, why can a StringBuffer be trusted while a char cannot? Because StringBuffer is written so as to pay attention to whether any Strings are sharing its internal char. If there are, then if the StringBuffer is subsequently modified it protects the shared char by first copying it and then modifying the copy. This means that the previously-shared char can now be left to be owned solely by the immatable String class that is using it. So basically, StringBuffer is trusted because the StringBuffer authors have made sure it will never change a char that is shared with a String.