Description
Write a program (method main) using an array to solve the following problem:
• Prompt the user to enter a String of data
• Use the length() method in order to make your code more scalable (hint research the java API for the String class – it is found in the java.lang package – this is where you will find information for the length() method, as well as the charAt() method).
• Using the charAt(index) method in the String class to access each character in the string individually, produce a count of the number of lower case letters, the count of the number of upper case letters, and a count of the miscellaneous letters.
• In order to produce the count specified previously, there are many ways that this can be achieved – see appendix at end of lab and make your choice. • Display the total number of letters in the string, and these counts
Example 1: (green is user input)
Enter the phrase:
Linda Crane
The total number of letters is 11
The number of upper case letters is 2
The number of lower case letters is 8
The number of other letters is 1
Example 2:
Enter the phrase:
1234ABCD efgh {}
The total number of letters is 16
The number of upper case letters is 4
The number of lower case letters is 4
The number of other letters is 8
Appendix: How to determine which characters are present in the String
NOTE: there are several different ways of solving this problem and for practice sake, I suggest that you try a few.
NOTE: At no time should you have 26 if statements or loops.
NOTE: You can easily google this question – but that won’t do you any good on a lab test or your exam if I ask a similar question…
a) Run through each of the characters in the phrase looking for ‘a’ or ‘A’, counting how many you have and display the result…then repeat for ‘b’ or ‘B’, etc. (Adding to the totals as appropriate).
b) Create an int array of 26 elements to hold the counts of each of the lower case letters in the alphabet and another to hold counts of upper case letters. Then, run through each of the characters in the phrase ONCE, adding 1 to the appropriate element in the array if the character in the phrase is a letter.
c) Use the ASCII code and implement a selection structure.
d) Research the Character class and find appropriate methods.
CST8110 – Lab Exercise 7 – arrays 1 of 1
Reviews
There are no reviews yet.