Java Program to Count the Total Number of Vowels and Consonants in a String Last Updated : 01 Feb, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 is present in the reference increment number of vowels by 1, otherwise, increment the number of consonants by 1. Example: Input : String = "GeeksforGeeks" Output: Number of Vowels = 5 Number of Consonants = 8 Input : String = "Alice" Output: Number of Vowels = 3 Number of Consonants = 2 Approach: Create two variables vow and cons and initialize them with 0.Start string traversing.If i'th character is vowel then increment in vow variable by 1.Else if the character is consonant then increment in cons variable by 1. Example Java // Java Program to Count Total Number of Vowels // and Consonants in a String // Importing all utility classes import java.util.*; // Main class class GFG { // Method 1 // To prints number of vowels and consonants public static void count(String str) { // Initially initializing elements with zero // as till now we have not traversed int vow = 0, con = 0; // Declaring a reference String // which contains all the vowels String ref = "aeiouAEIOU"; for (int i = 0; i < str.length(); i++) { // Check for any special characters present // in the given string if ((str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') || (str.charAt(i) >= 'a' && str.charAt(i) <= 'z')) { if (ref.indexOf(str.charAt(i)) != -1) vow++; else con++; } } // Print and display number of vowels and consonants // on console System.out.println("Number of Vowels = " + vow + "\nNumber of Consonants = " + con); } // Method 2 // Main driver method public static void main(String[] args) { // Custom string as input String str = "#GeeksforGeeks"; // Calling the method 1 count(str); } } OutputNumber of Vowels = 5 Number of Consonants = 8 Time Complexity: O(n²) here, n is the length of the string. Comment More infoAdvertise with us Next Article Java Program to Count the Total Number of Vowels and Consonants in a String Y yakksh Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Practice Tags : Java Similar Reads Java Program to Count Number of Vowels in a String 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 pac 4 min read Java Program to Count Number of Digits in a String The string is a sequence of characters. In java, objects of String are immutable. Immutable means that once an object is created, it's content can't change. Complete traversal in the string is required to find the total number of digits in a string. Examples: Input : string = "GeeksforGeeks password 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 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 Like