Commits

Anonymous committed 3083fea

eliminated one extra variable

  • Participants
  • Parent commits 8ba8e4d

Comments (0)

Files changed (1)

src/ShiftString.java

 		 * characters and save non-space character to an external
 		 * buffer
 		 */
-		int count = 0;
 		int nonspaces = 0;
 		for (int i = 0; i < string.length; i++) {
-			if (string[i] == ' ')
-				count++;
-			else {
+			if (string[i] != ' ')
 				buf[nonspaces++] = string[i];
-			}
 		}
 
+		int numberOfSpaces = string.length - nonspaces;
 		for (int i = buf.length - 1; i >= 0; i--) {
-			if (i >= count / 2 + nonspaces)
+			if (i >= numberOfSpaces / 2 + nonspaces)
 				buf[i] = ' ';
-			else if (i >= count / 2)
+			else if (i >= numberOfSpaces / 2)
 				/*
 				 * shift non-space character to the right by count / 2
 				 */
-				buf[i] = buf[i - count / 2];
+				buf[i] = buf[i - numberOfSpaces / 2];
 			else
 				buf[i] = ' ';
 		}