import java.text.DecimalFormat;

// A program to calculate statistics on text
public class Word {
    
    public static void main(String[] args) {
        int total,length;
        long sum;
        double average, sd;

        // for each String
        for (int j=0;j<5;j++) {
            String s=null;
            String stringName=null;
            // choose which String to process
            switch(j) {
            case 0:
                s = SomeStrings.s1;
                stringName = "s1";
                break;
            case 1:
                s = SomeStrings.s2;
                stringName = "s2";
                break;
            case 2:
                s = SomeStrings.s3;
                stringName = "s3";
                break;
            case 3:
                s = SomeStrings.s4;
                stringName = "s4";
                break;
            case 4:
                s = SomeStrings.s5;
                stringName = "s5";
                break;
            }
            
            sum=0;
            total=0;
            sd = 0.0;
            average = 0.0;
            length = s.length();
            // compute average first then the standard deviation
            boolean calcsd=false;
            do {
                // go through entire string
                for(int i=0;i<length;i++) {

                    // to find words we first skip over any whitespace
                    int current,skip;
                    skip=0;
                    while (i+skip<length && Character.isWhitespace(s.charAt(i+skip)))
                        skip++;

                    i+=skip;
                    // then we are at a word (or the end of the string)
                    // so we can count the size of the word
                    current=0;
                    while (i+current<length && !Character.isWhitespace(s.charAt(i+current)))
                        current++;
                    i+=current;

                    // now do our individual calculations, but only if there is a word
                    // (ie current length is greater than 0)
                    if (current>0) {
                        if (calcsd) {
                            // assume we already have the average
                            sd += Math.pow(current-average,2);
                        } else {
                            // we are calculating the average
                            total++;
                            sum += current;
                        }
                    }
                }
                if (!calcsd) {
                    average = (sum==0) ? 0.0 : (double)sum/total;
                }
                // switch from calculating average (calcsd=false) 
                // to sd (calcsd=true).
                // once we switch back to sd=false we exit the loop
                calcsd = !calcsd;
            } while(calcsd);
            
            // we want 8 digits to the right and 1 to the left of the decimal
            DecimalFormat dec = new DecimalFormat("0.00000000");
            // now to compute the standard deviation
            sd = (total==0) ? 0.0 : Math.sqrt((1.0/total)*sd);
            // and then output
            System.out.println("String: "+stringName);
            System.out.println("Average: "+dec.format(average));
            System.out.println("Standard deviation: "+dec.format(sd));
            System.out.println();
        }
    }
}
