class Ideone {
public static void main
( String [ ] args
) { test( "move these spaces to beginning" ) ;
test( "This is a test to see if it works ok." ) ;
}
public static void test
( String input
) { System .
out .
printf ( "Input : '%s'%nOutput: '%s'%n%n" ,
input, moveSpacesFirst( input) ) ;
}
char [ ] buf = input.toCharArray ( ) ;
int to = buf.length - 1 ;
for ( int from = buf.length - 1 ; from >= 0 ; from-- )
if ( buf[ from] != ' ' )
buf[ to-- ] = buf[ from] ;
while ( to >= 0 )
buf[ to-- ] = ' ' ;
}
}
Y2xhc3MgSWRlb25lIHsKCXB1YmxpYyBzdGF0aWMgdm9pZCBtYWluIChTdHJpbmdbXSBhcmdzKSB7CgkJdGVzdCgibW92ZSB0aGVzZSBzcGFjZXMgdG8gYmVnaW5uaW5nIik7CgkJdGVzdCgiVGhpcyBpcyBhIHRlc3QgdG8gc2VlIGlmIGl0IHdvcmtzIG9rLiIpOwoJfQoJcHVibGljIHN0YXRpYyB2b2lkIHRlc3QoU3RyaW5nIGlucHV0KSB7CgkJU3lzdGVtLm91dC5wcmludGYoIklucHV0IDogJyVzJyVuT3V0cHV0OiAnJXMnJW4lbiIsCgkJICAgICAgICAgICAgICAgICAgaW5wdXQsIG1vdmVTcGFjZXNGaXJzdChpbnB1dCkpOwoJfQoJcHVibGljIHN0YXRpYyBTdHJpbmcgbW92ZVNwYWNlc0ZpcnN0KFN0cmluZyBpbnB1dCkgewoJCWNoYXJbXSBidWYgPSBpbnB1dC50b0NoYXJBcnJheSgpOwoJCWludCB0byA9IGJ1Zi5sZW5ndGggLSAxOwoJCWZvciAoaW50IGZyb20gPSBidWYubGVuZ3RoIC0gMTsgZnJvbSA+PSAwOyBmcm9tLS0pCgkJCWlmIChidWZbZnJvbV0gIT0gJyAnKQoJCQkJYnVmW3RvLS1dID0gYnVmW2Zyb21dOwoJCXdoaWxlICh0byA+PSAwKQoJCQlidWZbdG8tLV0gPSAnICc7CgkJcmV0dXJuIG5ldyBTdHJpbmcoYnVmKTsKCX0KfQ==