created 05/26/03


Chapter 43 Programming Exercises

Exercise 1 --- Character Counter

Write a program that determines the number of consonants, vowels, punctuation characters, and spaces in an input line.

Read in the line into a String (in the usual way). Now use the charAt() method in a loop to access the characters one by one.

Use a switch statement to increment the appropriate variables based on the current character. After processing the line, print out the results.

Click here to go back to the main menu.


Exercise 2 --- Character Counter with File Input

One exercise one is working, modify it so that it reads in lines from a file, one by one. Use input redirection as explained in Chapter 22. Each line is processed using the logic of exercise one so that after the file is processed you have the count of consonants, vowels, punctuation characters, and spaces for the entire file.

After processing the file, print out the raw counts and also the percentages of each category in the file.

Click here to go back to the main menu.