➤ Sum of digits in a String
➤ Count No of Vowels String
➤ String Pattern Programs in Java
➤ Take String Input In Java using Scanner Class
➤ Take Multiple String Input in Java using Scanner
➤ How To Reverse a String In Java
➤ Java Remove Special Characters From String
➤ How To Remove Character From a String Java
➤ String Palindrome In Java
➤ Sort String In Java
➤ How to Compare Strings In Java
➤ Find Second Occurrence of Character in String
➤ Java Replace nth Occurrence String
➤ Find Last Occurrence of Character in String Java
➤ Check If String Contains Uppercase & Lowercase
➤ Java Check If Char is Uppercase
➤ Check If String is Uppercase Java
➤ Swap Characters in String Java
➤ Java String indexOf() Method
➤ How to Replace Dot in Java?
➤ How to Find Length of String in Java
➤ Substring Method In Java
➤ Split Method In Java
Java Program to Count Number of Vowels in a String | The alphabets ‘A’, ‘E’, ‘I’, ‘O’, ‘U’ (in uppercase) and ‘a’, ‘e’, ‘i’, ‘o’, ‘u’ are vowels and remaining alphabets are called consonants.
The given String can be in uppercase or lowercase or both, so either we have to write separate logic for both cases or convert the given string to uppercase or lowercase and write logic only for one case.
In the String class, toUpperCase() method is given to convert the given string to uppercase, and toLowerCase() method is given to convert string to lowercase.
// convert string to uppercase
String str = "[email protected]";
str = str.toUpperCase();
The toUpperCase() method converts “[email protected]” to “[email protected]”. Therefore, we have to write logic to check only for ‘A’, ‘E’, ‘I’, ‘O’, ‘U’. The charAt(int i) method of the String class can be used to iterate through each character of the String.
Condition to check character is vowel or not,
// in case of uppercase characters
if(ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
then it is vowel.
// in case of lowercase characters
if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
then it is vowel.
To count the number of vowels in the given String take a “count” variable of int data type and check each character. If any character is vowel then increase the “count” variable by 1. Finally, the count value has the total numbers of vowels in the given String.
Program to Count Vowels in Java String
import java.util.Scanner;
public class CountVowels {
public static void main(String[] args) {
// declare variables
Scanner scan = null;
String str = null;
int countVowel = 0;
// create Scanner class object
scan = new Scanner(System.in);
// read input
System.out.print("Enter String:: ");
str = scan.nextLine();
// convert string to upperCase
str = str.toUpperCase();
// check each character
for(int i=0; i < str.length(); i++) {
if(isVowel(str.charAt(i)))
countVowel++;
}
// display result
System.out.println("Number of vowels:: "
+ countVowel);
// close Scanner object
scan.close();
}
// method to check vowel (only uppercase)
private static boolean isVowel(char ch) {
if(ch == 'A' || ch == 'E' || ch == 'I'
|| ch == 'O' || ch == 'U') {
return true;
}
return false; // else return false;
}
}
Output:-
Enter String:: [email protected]
Number of vowels:: 3
Enter String:: Hello, How are you?
Number of vowels:: 7
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!
Similar Java Program Examples
- Check Even number
- Check Odd Number
- Java Program to Check Even Odd
- Find Greatest of three numbers
- Leap year program in Java
- Print Multiplication table
- Find Reverse of a number
- Find Factors of a number
- LCM of two numbers
- HCF of two numbers