Question

In: Computer Science

Write a JAVA program that reads in a string from standard input and determines the following:...

Write a JAVA program that reads in a string from standard input and determines the following:

- How many vowels are in the string (FOR THE PURPOSE OF THIS PROGRAM 'Y' is NOT considered a vowel)?

- How many upper case characters are in the string?

- How many digits are in the string?

- How many white space characters are in the string?

- Modify the program to indicate which vowel occurs the most. In the case of a tie, only output the vowel which comes first alphabetically. NOTE: if there are no vowels then output "no vowels"

Example input :

This String has vowels and 12345 digits.

Example output:

vowels = 8

upper = 2

digits = 5

whitespace = 6

vowel i occurs the most = 4

- Modify the program to output a list of all consecutively repeated characters. That is any character that repeats consecutively in the string.

Example input:

Some people raise raccoons for their pelts. A raccoon's home is called a nook. The person who cleans and tidies up the raccoonnooks is called the raccoonnookkeeper. Real word!

Example output:

vowels = 59

upper = 4

digits = 0

whitespace = 28

vowel o occurs the most = 21

repeated characters: c o c o l o c o n o l c o n o k e

NOTE: if there are no repeating characters then output "no repeating characters"

NOTE: by characters I mean ALPHABETIC characters, A - Z; not digits, not whitespace, not punctuation

Here's an example of what your output should look like for the A version if the input has NONE of the characters you are looking for.

vowels = 0

upper = 0

digits = 0

whitespace = 0

no vowels

no repeating characters

Solutions

Expert Solution

Hi,

Please find below code as per your requirement.

Let me know if you have any doubt/concerns in this via comments.

Hope this answer helps you.

Thanks.

/********************JAVA CODE********************/

/******************VowelCount.java******************/

import java.util.Scanner;

public class VowelCount {

        public static void main(String[] args) {

                //Scanner class used to take user input from console
                Scanner sc = new Scanner(System.in);
                //Prompts user to enter String
                System.out.print("Please enter String: ");
                //Reading string from console
                String input = sc.nextLine();

                //variables which keeps count of everything
                int vowels = 0, upper = 0, digits = 0, spaces = 0;
                int vowelA = 0,vowelE = 0,vowelI = 0,vowelO = 0,vowelU = 0;
                //StringBilder used to keep all consecutive repeating characters
                StringBuilder consectiveCharacters = new StringBuilder();

                //Iterating through each character of String
                for (int i = 0; i < input.length(); i++) {

                        //reading character from String at specified position into char
                        char ch = input.charAt(i);

                        //Checking if current char is vowel or not in lower as well upper case
                        if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' 
                                        || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U'){
                                vowels++;

                                //keeping count of individual vowels
                                if(ch == 'a' || ch == 'A') {
                                        vowelA++;
                                }

                                if(ch == 'e' || ch == 'E') {
                                        vowelE++;
                                }

                                if(ch == 'i' || ch == 'I') {
                                        vowelI++;
                                }

                                if(ch == 'o' || ch == 'O') {
                                        vowelO++;
                                }

                                if(ch == 'u' || ch == 'U') {
                                        vowelU++;
                                }

                        } 

                        //checking if char is Upper case alphabets
                        if (ch >= 'A' && ch <= 'Z') {
                                upper++;
                        } else if (ch >= '0' && ch <= '9') { //checking if char is digit
                                digits++;
                        } else if (ch == ' ') { //checking if char is spaces
                                spaces++;
                        }

                        //checking if current char and next char is same or not if yes it gets append to the StringBuilder object
                        if(i < (input.length() - 1) && ch == input.charAt(i + 1)) {
                                consectiveCharacters.append(ch + " ");
                        }

                }

                //Printing all counts - vowels,upper case alphabets,digits and whitespace
                System.out.println("Vowels= "+vowels);
                System.out.println("Upper= "+upper);
                System.out.println("Digits= "+digits);
                System.out.println("Whitespace= "+spaces);

                if(vowels == 0) { //if vowel count is zero
                        System.out.println("No Vowels");
                } else {
                        //Keeping all vowel count in array of int to find out max
                        int[] vowelArr = {vowelA,vowelE,vowelI,vowelO,vowelU};

                        //current max position
                        int maxPosition = 0;
                        for (int j = 0; j < vowelArr.length; j++) {
                                if(vowelArr[j] > vowelArr[maxPosition]) {
                                        //if current is greater than previous then update current max position
                                        maxPosition = j;
                                }
                        }
                        //checking max position based on char and maxcount
                        char maxOccuredVowel = ' ';
                        if(maxPosition == 0) {
                                maxOccuredVowel = 'a';
                        } else if(maxPosition == 1) {
                                maxOccuredVowel = 'e';
                        } else if(maxPosition == 2) {
                                maxOccuredVowel = 'i';
                        } else if(maxPosition == 3) {
                                maxOccuredVowel = 'o';
                        } else if(maxPosition == 4) {
                                maxOccuredVowel = 'u';
                        }

                        System.out.println("vowel "+maxOccuredVowel+" occurs the most = "+vowelArr[maxPosition]);

                }

                //Printing consecutive repeated characters
                if(consectiveCharacters.length() == 0) {
                        System.out.println("No Repeating characters");
                } else {
                        System.out.println("Repeated characters: "+consectiveCharacters.toString());
                }


        }

}

/*******************Output*******************/


Related Solutions

Write a Java program (name it PasswordTest) that reads from the user a string input (representing...
Write a Java program (name it PasswordTest) that reads from the user a string input (representing a password) and determines whether the password is “Valid Password” or “Invalid Password”. A valid password has at least 7 characters and includes at least one lower-case letter, at least one upper-case letter, at least one digit, and at least one character that is neither a letter nor a digit. Your program will need to check each character in the string in order to...
Using Perl language: Write a program that reads a string from the standard input, and uses...
Using Perl language: Write a program that reads a string from the standard input, and uses a Perl regular expression to test whether the string looks like a valid credit card number. The card numbers of four major companies follow the following formats: Visa 13 or 16 digits, starting with 4. MasterCard 16 digits, starting with 51 through 55. Discover 16 digits, starting with 6011 or 65. American Express 15 digits, starting with 34 or 37. You should test your...
Write a C++ program that reads a string from a text file and determines if the...
Write a C++ program that reads a string from a text file and determines if the string is a palindrome or not using stacks and queue
in. java Write a program that reads a string from the user, and creates another string...
in. java Write a program that reads a string from the user, and creates another string with the letters from in reversed order. You should do this using string concatenation and loops. Do not use any method from Java that does the work for you. The reverse string must be built. Do not just print the letters in reversed order. Instead, concatenate them in a string. --- Sample run: This program will create a string with letters in reversed order....
Instructions: Create a Java program that reads a string entered by the user and then determines...
Instructions: Create a Java program that reads a string entered by the user and then determines and prints how many of each lowercase vowel (a, e, i, o, and u) appear in the entire string. Have a separate counter for each vowel. Also, count and print the number of non-vowel characters. Example: User enters: "This house is beautiful." a: 1 e: 2 i: 3 o: 1 u: 2 non-vowel:10
Write a program that determines whether an input string is a palindrome; that is, whether it...
Write a program that determines whether an input string is a palindrome; that is, whether it can be read the same way forward and backward. At each point, you can read only one character of the input string; do not use an array to first store this string and then analyze it (except, possibly, in a stack implementation). Consider using multiple stacks. In Pseudocode please
Using Java: Write a program that uses hash tables and reads in a string from the...
Using Java: Write a program that uses hash tables and reads in a string from the command line and a dictionary of words from standard input, and checks whether it is a "good" password. Here, assume "good" means that it (i) is at least 8 characters long, (ii) is not a word in the dictionary, (iii) is not a word in the dictionary followed by a digit 0-9 (e.g., hello5), (iv) is not two words separated by a digit (e.g.,...
Write a C++ or Java program that reads an input graph data from a user. Then,...
Write a C++ or Java program that reads an input graph data from a user. Then, it should present a path for the travelling salesman problem (TSP). In the assignment, you can assume that the maximum number ofvertices in the input graph is less than or equal to 20. Input format: This is a sample input from a user. 4 12 0 1 2 0 3 7 0 2 5 1 0 2 1 2 8 1 3 3 2...
Write a Java program that reads an input graph data from a user. Then, it should...
Write a Java program that reads an input graph data from a user. Then, it should present a path for the travelling salesman problem (TSP). In the assignment, you can assume that the maximum number of vertices in the input graph is less than or equal to 20. Input format: This is a sample input from a user. 4 12 0 1 2 0 3 7 0 2 5 1 0 2 1 2 8 1 3 3 2 0...
Write a short main program that reads an integer n from standard input and prints (to...
Write a short main program that reads an integer n from standard input and prints (to standard output) n lines of * characters. The number of *’s per line should double each time, starting with 1. E.g., if n = 5, the output should be as follows: * ** **** ******** ****************
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT