//  Data Structures with Java by John R. Hubbard
//  Copyright McGraw-Hill, 2001
//  Problem 1.11 on page 21


public class Pr0111
{ public static void main(String[] args)
  { System.out.println("format(\"tomato\",9,-1) = [" + format("tomato",9,-1) + "]");
    System.out.println("format(\"tomato\",9, 0) = [" + format("tomato",9, 0) + "]");
    System.out.println("format(\"tomato\",9, 1) = [" + format("tomato",9, 1) + "]");
  }
  
  public static String format(String s, int len, int d)
  { int spaces = (len - s.length());
    if (spaces <= 0) return s;
    String formatS = "";
    int leftSpaces = (d<0)? 0 : ((d>0)? spaces : spaces/2);
    int rightSpaces = spaces - leftSpaces;
    for (int i=0; i<leftSpaces; i++)
      formatS += " ";
    formatS += s ;  // s comes after spaces on left
    for (int i=0; i<rightSpaces; i++)
      formatS += " ";
    return formatS;
  }
}
