Anonymous avatar Anonymous committed 44fc245

switched shift2 and shift3
cleaned up shift function so it's easier to read
added comments

Comments (0)

Files changed (1)

src/ShiftString.java

+/**
+ * Extract non-space characters from a string and shift them to the
+ * middle of the string. For example, "I am Sam" should become
+ * " IamSam ".
+ *
+ * @author AbsoluteNoob
+ *
+ */
 public class ShiftString {
 
+	/**
+	 * Extract non-space characters from a string and shift them to
+	 * the middle of the string.
+	 *
+	 * This function scan the input string count number of spaces and
+	 * non-spaces characters and save non-space character to an
+	 * external buffer.
+	 *
+	 * @param input
+	 *            input string
+	 * @return string with space characters at left or right end of
+	 *         the string and non-space characters in the middle
+	 */
 	public static String shift(String input) {
 		char[] string = input.toCharArray();
 		char[] buf = new char[string.length];
 
+		/*
+		 * scan input string. count number of spaces and non-space
+		 * characters and save non-space character to an external
+		 * buffer
+		 */
 		int count = 0;
 		int nonspaces = 0;
-		for (int i = string.length - 1; i >= 0; i--) {
+		for (int i = 0; i < string.length; i++) {
 			if (string[i] == ' ')
 				count++;
 			else {
-				buf[buf.length - 1 - nonspaces] = string[i];
-				nonspaces++;
+				buf[nonspaces++] = string[i];
 			}
 		}
 
-		for (int i = 0; i < buf.length; i++) {
-			if (i <= count / 2)
+		for (int i = buf.length - 1; i >= 0; i--) {
+			if (i >= count / 2 + nonspaces)
 				buf[i] = ' ';
-			else if (i <= count / 2 + nonspaces)
-				buf[i] = buf[i + count - count / 2 - 1];
+			else if (i >= count / 2)
+				/*
+				 * shift non-space character to the right by count / 2
+				 */
+				buf[i] = buf[i - count / 2];
 			else
 				buf[i] = ' ';
 		}
 		return String.valueOf(buf);
 	}
 
+	/**
+	 * Extract non-space characters from a string and shift them to
+	 * the middle of the string.
+	 *
+	 * In-place version of shift function. It scan the input string
+	 * and shift all non-space characters to the beginning of the
+	 * buffer.
+	 *
+	 * @param input
+	 *            input string
+	 * @return string with space characters at left or right end of
+	 *         the string and non-space characters in the middle
+	 */
 	public static String shift2(String input) {
 		char[] string = input.toCharArray();
-		boolean shiftToRight = false;
-		int leftSpaces = 0, rightSpaces = 0;
-		int index = 0;
-
-		while (index < string.length - rightSpaces) {
-			if (string[index] == ' ') {
-				if (shiftToRight) {
-					for (int i = index; i > leftSpaces; i--) {
-						string[i] = string[i - 1];
-					}
-					string[leftSpaces++] = ' ';
-					shiftToRight = !shiftToRight;
-					++index;
-				} else {
-					for (int i = index; i < string.length - 1
-							- rightSpaces; i++) {
-						string[i] = string[i + 1];
-					}
-
-					string[string.length - 1 - rightSpaces] = ' ';
-					++rightSpaces;
-					shiftToRight = !shiftToRight;
-				}
-			} else {
-				++index;
-			}
-		}
-
-		return String.valueOf(string);
-	}
-
-	public static String shift3(String input) {
-		char[] string = input.toCharArray();
 		int index = 0, nonspaces = 0;
 		while (index < string.length) {
 			if (string[index] == ' ') {
 				++index;
 			} else {
+				/*
+				 * shift non-space characters to the beginning of the
+				 * buffer
+				 */
 				string[nonspaces++] = string[index++];
 			}
 		}
 		return String.valueOf(string);
 	}
 
+	/**
+	 * Extract non-space characters from a string and shift them to
+	 * the middle of the string.
+	 *
+	 * when encounter a space character, shift the non-space
+	 * characters left of the space to the right or right of the space
+	 * to the left in turns.
+	 *
+	 * @param input
+	 *            input string
+	 * @return string with space characters at left or right end of
+	 *         the string and non-space characters in the middle
+	 */
+	public static String shift3(String input) {
+		char[] string = input.toCharArray();
+		boolean shiftToRight = false;
+		// number of space character at right/left end of the string
+		int leftSpaces = 0, rightSpaces = 0;
+		int index = 0;
+
+		while (index < string.length - rightSpaces) {
+			if (string[index] == ' ') {
+				if (shiftToRight) {
+					/*
+					 * shift non-space characters left of the space
+					 * character to the right
+					 */
+					for (int i = index; i > leftSpaces; i--) {
+						string[i] = string[i - 1];
+					}
+					string[leftSpaces++] = ' ';
+					shiftToRight = !shiftToRight;
+					++index;
+				} else {
+					/*
+					 * shift non-space characters right of the space
+					 * character to the left
+					 */
+					for (int i = index; i < string.length - 1
+							- rightSpaces; i++) {
+						string[i] = string[i + 1];
+					}
+
+					string[string.length - 1 - rightSpaces] = ' ';
+					++rightSpaces;
+					shiftToRight = !shiftToRight;
+				}
+			} else {
+				++index;
+			}
+		}
+
+		return String.valueOf(string);
+	}
+
 	public static void main(String[] args) {
 		String str = "  I am  AbsoluteNoob  ";
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.