Question

Java: Assume letters A E O U I as the vowels. Write a program that prompts...

Java: Assume letters A E O U I as the vowels. Write a program that prompts the user to enter a string and displays the number of vowels and consonants in the string. The same vowels or consonants are counted only once. Use sets in the code.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
//Main.java
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Main {
  public static void main(String [] args) {
    Set<Character> hash_Set = new HashSet<Character>();
    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter string: ");
    String s = scanner.nextLine();

    for(char ch: s.toCharArray()){
      hash_Set.add(ch);
    }

    int vowels = 0, cons = 0;

    for (char ch : hash_Set) {
      if ((ch >= 'A' && ch <= 'Z') || ((ch >= 'a' && ch <= 'z'))) {
        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
          vowels++;
        } else {
          cons++;
        }
      }
    }

    System.out.println("Vowels count = " + vowels);
    System.out.println("Consonants count = " + cons);

  }
}

Add a comment
Know the answer?
Add Answer to:
Java: Assume letters A E O U I as the vowels. Write a program that prompts...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • in C++ please. (Vowel or consonant?) Assume letters A/a, E/e, I/i,0/o, and U/u as the vowels...

    in C++ please. (Vowel or consonant?) Assume letters A/a, E/e, I/i,0/o, and U/u as the vowels Write a program that prompts the user to enter a letter and check whether the letter is a vowel or consonant. Here is a sample run: 4.10 Enter a letter: B B is a consonant -Enter Enter a letter grade: a is a vowel Enter Enter a letter grade: # is an invalid input Enter (Vowel or consonant?) Assume letters A/a, E/e, I/i,0/o, and...

  • (Count vowels and consonants, file input, nested-loops, switch statement) Assume that letters A, E, I, O...

    (Count vowels and consonants, file input, nested-loops, switch statement) Assume that letters A, E, I, O and U are the vowels. Write a program that reads strings from a text file, one line at a time, using a while-loop. You do the following operations within each loop:  Read one line from the input file and store it in a string;  Count the number of vowels and consonants (using either while-loop or for-loop) in the file string. The while-loop...

  • C++ program by netBeans java language Exercise #2: Write a java program that prompts the user...

    C++ program by netBeans java language Exercise #2: Write a java program that prompts the user to enter a sentence. The program has to find the print: a. the position of vowels in the sentence. b. the number of vowels in the sentence (A, a, U, u, E, e, 0, o, I, i) c. the number of characters as numbers or special characters (other than English letters a..z, A..Z). Hint: remember to ignore the spaces. Sample input-output: Enter the sentnse:...

  • Exercise #3: write a Java program that prompts the user to enter a sentence. The program...

    Exercise #3: write a Java program that prompts the user to enter a sentence. The program has to find the print: a. the position of vowels in the sentence. b. the number of vowels in the sentence (A, a, U, u, E, e, O, o, I, i) c. the number of characters as numbers or special characters (other than English letters a.z, A..Z). Hint: remember to ignore the spaces. Sample input-output: Enter the sentnse: UAEU is the university of the...

  • Vowel or Consonant?Write a program for Programming Exercise 4.10 on p.151 in the textbook. Testing: Run...

    Vowel or Consonant?Write a program for Programming Exercise 4.10 on p.151 in the textbook. Testing: Run the program for two vowels, two consonants, and two invalid characters. Assume letters A/a , E/e , I/i , O/o , U/u as the vowels. Write a program that prompts the user to enter a letter and check whether the letter is a vowel or constant.

  • String variables/Selection & loop Write a complete Java program which prompts the user for a sentence on one line wh...

    String variables/Selection & loop Write a complete Java program which prompts the user for a sentence on one line where each word is separated by one space, reads the line into one String variable using nextline(), converts the string into Ubbi dubbi and displays the translated sentence. Ubbi dubbi works by adding ub before each vowel sound in a syllable. For example, hello becomes hubellubo. The word speak has the vowel sound ea, so in Ubbi dubbi it becomes spubeak....

  • Write a program which gives the user the option to separate, Vowels or Consonant from a...

    Write a program which gives the user the option to separate, Vowels or Consonant from a given text from a file. We will display the separated data on screen and also store it in output file. This is for C++ . Name of the input file to be used is: SeparateConVow.dat . The input file has this sentence: THIS IS THE FINAL PROJECT FOR OUR CLASS . The code should separate the vowels and consonants in that sentence. ^ ....

  • ‘C’ programming language question Write a MENU DRIVEN program to A) Count the number of vowels...

    ‘C’ programming language question Write a MENU DRIVEN program to A) Count the number of vowels in the string B) Count the number of consonants in the string C) Convert the string to uppercase D) Convert the string to lowercase E) Display the current string X) Exit the program The program should start with a user prompt to enter a string, and let them type it in. Then the menu would be displayed. User may enter option in small case...

  • Write a program that prompts the user for a string and stores the user's string into...

    Write a program that prompts the user for a string and stores the user's string into a variable using getline(). After receiving the string, the program counts and displays the number of vowels in the string. Then, the program should prompt the user if they would like to enter another string.

  • I need this in Visual Studio C++ Write a function that count the number of vowels,...

    I need this in Visual Studio C++ Write a function that count the number of vowels, the number of consonants and the average number of letters in each word. The function accept a C-String (char array) or a string object as an argument and return the number of vowels, number of consonants and the average number of letters in each word. Problem: Requirements: . Use pointers as part of the solution Read a string from a text file. . Write...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT