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 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
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
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...
Q20. Using C++ style string to write a program that reads a sentence as input and...
Q20. Using C++ style string to write a program that reads a sentence as input and converts each word of the sentence following the rule below: For every word in the sentence, the first letter is relocated the end of the word. Then append the string “KPU” to the word. More requirements: All letters in the output should be uppercase. More assumptions: The input sentence contains no non-alphabetic letters Sample Run: Please enter the original sentence: i LOVE to program...
Q20. Using C++ style string to write a program that reads a sentence as input and...
Q20. Using C++ style string to write a program that reads a sentence as input and converts each word of the sentence following the rule below: For every word in the sentence, the first letter is relocated the end of the word. Then append the string “KPU” to the word. More requirements: All letters in the output should be uppercase. More assumptions: The input sentence contains no non-alphabetic letters Sample Run: Please enter the original sentence: i LOVE to program...
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: * ** **** ******** ****************
Write a program called x.c that reads an integer n from standard input, and prints an...
Write a program called x.c that reads an integer n from standard input, and prints an nxn pattern of asterisks and dashes in the shape of an "X". You can assume n is odd and >= 5. Solve this problem using only while loop. Solution: ./x Enter size: 5 *---* -*-*- --*-- -*-*- *---* ./x Enter size: 9 *-------* -*-----*- --*---*-- ---*-*--- ----*---- ---*-*--- --*---*-- -*-----*- *-------* ./x Enter size: 15 *-------------* -*-----------*- --*---------*-- ---*-------*--- ----*-----*---- -----*---*----- ------*-*------ -------*------- ------*-*------...
2. Write a Java program that reads a series of input lines from given file “name.txt”,...
2. Write a Java program that reads a series of input lines from given file “name.txt”, and sorts them into alphabetical order, ignoring the case of words. The program should use the merge sort algorithm so that it efficiently sorts a large file. Contents of names.text Slater, KendallLavery, RyanChandler, Arabella "Babe"Chandler, StuartKane, EricaChandler, Adam JrSlater, ZachMontgomery, JacksonChandler, KrystalMartin, JamesMontgomery, BiancaCortlandt, PalmerDevane, AidanMadden, JoshHayward, DavidLavery,k JonathanSmythe, GreenleeCortlandt, OpalMcDermott, AnnieHenry, DiGrey, MariaEnglish, BrookeKeefer, JuliaMartin, JosephMontgomery, LilyDillon, AmandaColby, LizaStone, Mary FrancesChandler, ColbyFrye, DerekMontgomery,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT