Μετρητής λέξεων

import gr.aueb.dds.BIO;

/*
 * Count number of words.
 * We define a word as a sequence of latin alphabetic characters.
 * Demonstrates the use of toggle variables to hold state.
 */
class WordCount {
        public static void main(String args[]) {
                int code;
                char c;
                boolean inWord = false;         // True when scanning a word
                int count = 0;

                while ((code = BIO.readChar()) != -1) {
                        c = (char)code;
                        if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
                                // Word character
                                if (!inWord)
                                        inWord = true;
                        } else {
                                // Non word character
                                if (inWord) {
                                        // A word just finished
                                        inWord = false;
                                        count++;
                                }
                        }
                }
                BIO.println(count);
        }
}