Write a Java Program to Count Vowels and Consonants in a String with an example. In this Java count consonants and vowels in a string example, we first used for loop to iterate vowConsStr. Inside the loop, we assigned (ch = vowConsStr.charAt(i)) each character to ch to keep the code simple. Next, we used the Else If statement.
Within the if condition, we check whether the character is equal o a, e, i o, u, A, E, I, O, and U. If true, we increment the value of the vowel. In the else if statement, we used (ch >= ‘a’ && ch <= ‘z’ || ch >= ‘A’ && ch <= ‘Z’) to check whether the character is an alphabet. If True, we increment the consonant value. Here, we can also use else if (Character.isAlphabetic(ch)).
import java.util.Scanner;
public class CountVowCons1 {
private static Scanner sc;
public static void main(String[] args) {
String vowConsStr;
int i, vowels, consonants;
vowels = consonants = 0;
char ch;
sc= new Scanner(System.in);
System.out.print("\nPlease Enter Vowel, Consonant String = ");
vowConsStr = sc.nextLine();
for(i = 0; i < vowConsStr.length(); i++)
{
ch = vowConsStr.charAt(i);
if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
vowels++;
}
else if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z'){
consonants++;
}
}
System.out.println("\nNumber of Vowel Characters = " + vowels);
System.out.println("Number of Consonant Characters = " + consonants);
}
}
OUTPUT
Java Program to Count Vowels and Consonants in a String using While Loop
The following toLowerCase() statement converts the given string to lowercase.
String lw_vowConsStr = vowConsStr.toLowerCase();
Instead of checking both uppercase and lowercase letters, we compare only lowercase and counting the vowels and consonants.
import java.util.Scanner;
public class CountVowCons2 {
private static Scanner sc;
public static void main(String[] args) {
String vowConsStr;
int i, vowels, consonants;
i = vowels = consonants = 0;
char ch;
sc= new Scanner(System.in);
System.out.print("\nPlease Enter Vowel, Consonant String = ");
vowConsStr = sc.nextLine();
String lw_vowConsStr = vowConsStr.toLowerCase();
while(i < lw_vowConsStr.length())
{
ch = lw_vowConsStr.charAt(i);
if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
vowels++;
}
else if(ch >= 'a' && ch <= 'z') {
consonants++;
}
i++;
}
System.out.println("\nNumber of Vowel Characters = " + vowels);
System.out.println("Number of Consonant Characters = " + consonants);
}
}
RESULT
In this Java Count Consonants and Vowels in a String example, we compare the ASCII values instead of comparing characters.
import java.util.Scanner;
public class CountVowCons3 {
private static Scanner sc;
public static void main(String[] args) {
String vowConsStr;
int i, vowels, consonants;
vowels = consonants = 0;
int cp;
sc= new Scanner(System.in);
System.out.print("\nPlease Enter Vowel, Consonant String = ");
vowConsStr = sc.nextLine();
String up_vowConsStr = vowConsStr.toUpperCase();
for(i = 0; i < up_vowConsStr.length(); i++)
{
cp = up_vowConsStr.codePointAt(i);
if(cp == 65 || cp == 69 || cp == 73 || cp == 79 || cp == 85) {
vowels++;
}
else if(cp >= 65 && cp <= 90 || cp >= 97 && cp <= 122) {
consonants++;
}
}
System.out.println("\nNumber of Vowel Characters = " + vowels);
System.out.println("Number of Consonant Characters = " + consonants);
}
}
OUTPUT
This Java code to Count String Vowels and Consonants is the same as the above. Here, we separated the Vowels and Consonants logic using Java functions.
import java.util.Scanner;
public class CountVowCons4 {
private static Scanner sc;
public static void main(String[] args) {
String vowConsStr;
sc= new Scanner(System.in);
System.out.print("\nPlease Enter Vowel, Consonant String = ");
vowConsStr = sc.nextLine();
VowOrCons(vowConsStr.toLowerCase());
}
public static void VowOrCons (String vowConsStr) {
int i, vowels, consonants;
vowels = consonants = 0;
char ch;
for(i = 0; i < vowConsStr.length(); i++)
{
ch = vowConsStr.charAt(i);
if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
vowels++;
}
else if(ch >= 'a' && ch <= 'z') {
consonants++;
}
}
System.out.println("\nNumber of Vowel Characters = " + vowels);
System.out.println("Number of Consonant Characters = " + consonants);
}
}
RESULT