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
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*******************/
Get Answers For Free
Most questions answered within 1 hours.