Find Vowels in a String

In this post, we will see how to find and count vowels in a string.

Find Vowels in a String

If any character in String satisfy below condition then it is vowel and we will add it to Hashset.

character==’a’ || character==’A’ || character==’e’ || character==’E’ ||
character==’i’ || character==’I’ || character==’o’ || character==’O’ ||
character==’u’ || character==’U’

Output:

Enter an String : Java2blog
Vowels are:
a o

Explanation

  1. Iterate over String str and check each Character is vowel or not based on below condition
  2. If it is vowel, then print the vowel

Count number of Vowels in the String

Here is the program to count number of Vowels in the String.

Output:

Enter an String : Java2blog
Number of vowels: 3

Explanation

  1. Declare count variable and initialize it with 0.
  2. Iterate over String str and check each Character is vowel or not based on below condition
  3. If it is vowel, then increment the count
  4. Once loop is complete, then return the count.

That’s all about how to find vowels in a string in java.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *