Question

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

Homework Answers

Answer #1

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*******************/

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write an Assembler Program to input a string from the keyboard, store the string in memory....
Write an Assembler Program to input a string from the keyboard, store the string in memory. Your program should check and display if the string contains vowel letters (A, E, I, O, U), Hint: your program should check both lower and upper case
1 Design and implement FileCompare program that compares two text input files (file1.txt and file2.txt), line-by-line,...
1 Design and implement FileCompare program that compares two text input files (file1.txt and file2.txt), line-by-line, for equality. Print any lines that are not equivalent indicating the line numbers in both files. The language of implementation is java 2 . Create a program that reads a string input from the user, then determines and prints how many of each lowercase vowels (a, e. i, o, and u) appear in the entire string. Have a separate counter for each vowel. Also...
Write a program that takes a string of characters (including spaces) as input, computes the frequency...
Write a program that takes a string of characters (including spaces) as input, computes the frequency of each character, sorts them by frequency, and outputs the Huffman code for each character.   When you are writing your program, you should first test it on a string of 7 characters, so you can check it. PLEASE NOTE: Your program must work for any text that has upper and lower case letters digits 0 - 9, commas, periods, and spaces. Please submit the...
Write a Python program to ask user input of a string, then ask user input of...
Write a Python program to ask user input of a string, then ask user input of what letters they want to remove from the string. User can either put in "odd", "even" or a number "n" that is greater than 2. When user input "odd", it will remove the characters which have odd numbers in the sequence of the string. When user input "even", it will remove the characters which have even numbers in the sequence of the string. When...
Write a program that reads a string and outputs the number of lowercase vowels in the...
Write a program that reads a string and outputs the number of lowercase vowels in the string. Your program must contain a function with a parameter of a char variable that returns an int. The function will return a 1 if the char being passed in is a lowercase vowel, and a 0 for any other character. The output for your main program should be: There are XXXX lowercase vowels in string yyyyyyyyyyyyyyyyyyyyyy Where XXXX is the count of lowercase...
Write an assembly program that reads characters from standard input until the “end of file” is...
Write an assembly program that reads characters from standard input until the “end of file” is reached. The input provided to the program contains A, C, T, and G characters. The file also may have new line characters (ASCII code 10 decimal), which should be skipped/ignored. The program then must print the count for each character. You can assume (in this whole assignment) that the input doe not contain any other kinds of characters.  the X86 assembly program that simply counts...
Write a C program that counts the number of repeated characters in a phrase entered by...
Write a C program that counts the number of repeated characters in a phrase entered by the user and prints them. If none of the characters are repeated, then print “No character is repeated” For example: If the phrase is “full proof” then the output will be: Number of characters repeated: 3 Characters repeated: f, l, o Note: Assume the length of the string is 10.
Write a Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
Part 1 Write a program that reads a line of input and display the characters between...
Part 1 Write a program that reads a line of input and display the characters between the first two '*' characters. If no two '*' occur, the program should display a message about not finding two * characters. For example, if the user enters: 1abc*D2Efg_#!*345Higkl*mn+op*qr the program should display the following: D2Efg_#! 1) Name your program stars.c. 2) Assume input is no more than 1000 characters. 3) String library functions are NOT allowed in this program. 4) To read a...
Write a program that accepts an input string from the user and converts it into an...
Write a program that accepts an input string from the user and converts it into an array of words using an array of pointers. Each pointer in the array should point to the location of the first letter of each word. Implement this conversion in a function str_to_word which returns an integer reflecting the number of words in the original string. To help isolate each word in the sentence, convert the spaces to NULL characters. You can assume the input...