fork download
  1. class Ideone {
  2. public static void main (String[] args) {
  3. test("move these spaces to beginning");
  4. test("This is a test to see if it works ok.");
  5. }
  6. public static void test(String input) {
  7. System.out.printf("Input : '%s'%nOutput: '%s'%n%n",
  8. input, moveSpacesFirst(input));
  9. }
  10. public static String moveSpacesFirst(String input) {
  11. char[] buf = input.toCharArray();
  12. int to = buf.length - 1;
  13. for (int from = buf.length - 1; from >= 0; from--)
  14. if (buf[from] != ' ')
  15. buf[to--] = buf[from];
  16. while (to >= 0)
  17. buf[to--] = ' ';
  18. return new String(buf);
  19. }
  20. }
Success #stdin #stdout 0.1s 321280KB
stdin
Standard input is empty
stdout
Input : 'move these spaces to beginning'
Output: '    movethesespacestobeginning'

Input : 'This is a test to see if it works ok.'
Output: '         Thisisatesttoseeifitworksok.'