Squeeze.java


Below is the syntax highlighted version of Squeeze.java from §3.1 Using Data Types.



/*************************************************************************
 *  Compilation:  javac Squeeze.java
 *  Execution:    java Squeeze
 *  Dependencies: In.java
 *
 *  Takes a string command line argument and removes adjacent spaces.
 *  
 *  % java Squeeze "this is    a    test"
 *  this is a test
 *
 *************************************************************************/

public class Squeeze { 
    public static String squeeze(String s) { 
        char[] a = s.toCharArray();
        int N = 1;
        for (int i = 1; i < a.length; i++) { 
            a[N] = a[i];
            if      (a[N]   != ' ') N++;
            else if (a[N-1] != ' ') N++;
        }
        return new String(a, 0, N);
    }


    public static void main(String[] args) { 
        System.out.println(squeeze(args[0]));
    }

}


Copyright © 2000–2010, Robert Sedgewick and Kevin Wayne.
Last updated: Wed Feb 9 09:07:43 EST 2011.