001package algs51; // section 5.0
002import stdlib.*;
003/* ***********************************************************************
004 *  Compilation:  javac Squeeze.java
005 *  Execution:    java Squeeze
006 *  Dependencies: In.java
007 *
008 *  Takes a string command line argument and removes adjacent spaces.
009 *
010 *  % java Squeeze "this is    a    test"
011 *  this is a test
012 *
013 *************************************************************************/
014
015public class XSqueeze {
016        public static String squeeze(String s) {
017                char[] a = s.toCharArray();
018                int N = 1;
019                for (int i = 1; i < a.length; i++) {
020                        a[N] = a[i];
021                        if      (a[N]   != ' ') N++;
022                        else if (a[N-1] != ' ') N++;
023                }
024                return new String(a, 0, N);
025        }
026
027
028        public static void main(String[] args) {
029                args = new String[] { "this is    a    test" };
030                StdOut.println(squeeze(args[0]));
031        }
032
033}