// Java app for Homework #1 // Dan J. Fraser (1219229) import java.io.*; public class Size { public static void main(String args[]) { char c; int linesize = 0; do { StringBuffer InputLine = new StringBuffer(); try { // force a byte into a char. // this is ugly... should use unicode readers... // suggested input routine from textbook: while ((c = (char)System.in.read()) != '\n') { InputLine.append(c); } } catch (IOException e1) { System.out.println("Couldn't read from stdin: " + e1.getMessage()); } linesize = SizeOfLine(InputLine.toString()); if (linesize > 0) System.out.println("line size was " + linesize); } while (linesize != 0); System.out.println("bye"); }; public static int SizeOfLine(String line) { int OrigLength = line.length(); for (int i = 0; i < line.length() ; i++) // walk forward if (line.charAt(i) == ' ') // look for space OrigLength--; // if we have a space, subtract 1 from the length else break; // if we dont have a space, stop. // if the string is empty, we dont need to go though it again if (OrigLength != 0) for (int i = (line.length() - 1); i >= 0 ; i--) // walk backward. if (line.charAt(i) == ' ') // look for space OrigLength--; // if we have a space, subtract 1 from the length else break; // if we dont have a space, stop! return OrigLength; } };