Java Program to Count Number of Vowels in a String Last Updated : 02 Nov, 2022 Comments Improve Suggest changes Like Article Like Report In java, the string is a sequence of characters and char is a single digit used to store variables. The char uses 2 bytes in java. In java, BufferedReader and InputStreamReader are used to read the input given by the user from the keyboard. Then readLine() is used for reading a line. The java.io package in java provides input and output through data streams, serialization, and the file system. We can count the vowels in a string in two ways: IterativeRecursive Examples: Input: GeeksForGeeks Output: Total no of vowels in string are: 5 Input: ChETaN Output: Total no of vowels in string are: 2 Method 1: Iterative Approach: We will traverse through the string's characters in a for loop starting from index 0 till size-1.And check each character if it is a vowel or not and increment the count variable. Below is the implementation of the above approach: Java // Java Program to Count Number of Vowels // in a String in a iterative way import java.io.*; public class vowel { public static void main(String[] args) throws IOException { String str = "GeeksForGeeks"; str = str.toLowerCase(); int count = 0; for (int i = 0; i < str.length(); i++) { // check if char[i] is vowel if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o' || str.charAt(i) == 'u') { // count increments if there is vowel in // char[i] count++; } } // display total count of vowels in string System.out.println( "Total no of vowels in string are: " + count); } } OutputTotal no of vowels in string are: 5 Method 2: RecursiveApproach: Check for the base condition if the length of the string is 1, then simply check for that single character if it is a vowel, then return 1 else return 0.For dividing the whole string into substrings to return the answer recursively, we will get the answer for the string starting from the first till second last character.And finally, return the above answer plus the answer for the check of last character (1 if it is vowel or 0 if it is not) Below is the implementation of the above approach: Java // Java Program to Count Number of Vowels // in a String in a recursive way import java.io.*; class GFG { // isVowel() function returns 1 if the // character is a vowel and 0 if it is not static int isVowel(char chars) { if (chars == 'a' || chars == 'e' || chars == 'i' || chars == 'o' || chars == 'u') { return 1; } else { return 0; } } // recursive function to return the number // of characters in a string static int vowelno(String str, int l) { if (l == 1) { return isVowel(str.charAt(l - 1)); } return vowelno(str, l - 1) + isVowel(str.charAt(l - 1)); } public static void main(String[] args) throws IOException { String str = "BufferedOutput"; str = str.toLowerCase(); System.out.println( "Total number of vowels in string are:"); System.out.println(vowelno(str, str.length())); } } OutputTotal number of vowels in string are: 6 Method 3: Using ArrayList and contains() method Java // Java Program to Count Number of Vowels // in a String import java.io.*; import java.util.*; public class Main{ public static void main(String[] args)throws IOException { String str = "GeeksForGeeks"; str = str.toLowerCase(); int count = 0; String vow ="aeiou"; ArrayList<Character> vowels = new ArrayList<Character>(); for(int i=0;i<vow.length();i++) { vowels.add(vow.charAt(i)); } for (int i = 0; i < str.length(); i++) { if(vowels.contains(str.charAt(i))){ count++; } } // display total count of vowels in string System.out.println("Total no of vowels in string are: " + count); } } OutputTotal no of vowels in string are: 5 Comment More infoAdvertise with us Next Article Java Program to Count Number of Vowels in a String chetanjha888 Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Java-String-Programs +1 More Practice Tags : Java Similar Reads Java Program to Count the Total Number of Vowels and Consonants in a String Given a String count the total number of vowels and consonants in this given string. Assuming String may contain only special characters, or white spaces, or a combination of all. The idea is to iterate the string and checks if that character is present in the reference string or not. If a character 2 min read Java program to count the occurrence of each character in a string using Hashmap Given a string, the task is to write a program in Java which prints the number of occurrences of each character in a string. Examples: Input: str = "GeeksForGeeks" Output: r 1 s 2 e 4 F 1 G 2 k 2 o 1 Input: str = "Ajit" Output: A 1 t 1 i 1 j 1 An approach using frequency[] array has already been dis 2 min read Program to remove consonants from a String Given a string , the task is to remove all the consonants from the string and then print the string. Examples: Input: str= "Welcome to geeksforgeeks" Output: eoe o eeoee Input: str= "What is your name?" Output: a i ou ae? Recommended PracticeRemove consonants from a stringTry It!Approach: Traverse a 6 min read Java Program to Count the Occurrences of Each Character In Java, counting the occurrences of each character in a string is a fundamental operation that can be done in different ways. This process involves identifying the frequency of each character in the input string and displaying it in a readable format.Example: Input/output to count the occurrences o 5 min read Java Program to Find All Palindromic Sub-Strings of a String Given a string, the task is to count all palindrome substring in a given string. Input : aba Output : 4 Explanation : All palindrome substring are : "aba" , "a" , "b", "a" Input : TENET Output : 7 Explanation : All palindrome sub-string are : "T" , "E" , "N", "E", "T" , "ENE" , "TENET" Approach: Tak 2 min read Java Program to Check Whether the Character is Vowel or Consonant In this article, we are going to learn how to check whether a character is a vowel or a consonant. So, the task is, for any given character, we need to check if it is a vowel or a consonant. As we know, vowels are âaâ, âeâ, âiâ, âoâ, âuâ and all the other characters (i.e. âbâ, âcâ, âdâ, âfâ â¦..) are 4 min read Java program to count the characters in each word in a given sentence Write a Java program to count the characters in each word in a given sentence? Examples: Input : geeks for geeksOutput :geeks->5for->3geeks->5 Recommended: Please solve it on PRACTICE first, before moving on to the solution. Approach:Here we have to find out number of words in a sentence an 3 min read How to Validate if a String Starts with a Vowel Using Regex in Java? Regular Expressions in Java allow developers to create patterns for matching strings. In this article, we will learn How to Check whether the Given Character String Starts with a Vowel. Example to Check String is Starting with a VowelInput: "Apple"Output: Is a VowelInput: "Cart"Output: Is not a Vowe 2 min read PHP Program to Count Number of Vowels in a String Given a String, the task is to count the number of Vowels in a given string in PHP. Counting the number of vowels in a string is a common programming task, often encountered in text processing and data analysis. Here, we will cover three common scenarios for counting the number of Vowels in a String 3 min read R Program to Count the Number of Vowels in a String In this article, we will discuss how to Count the Number of Vowels in a String with its working example in the R Programming Language. It is a fundamental in programming that allows us to repeatedly execute a block of code as long as a specified condition remains true. It's often used for tasks like 5 min read Like