import java.util.Scanner; public class VowelConsonantCounter { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter a string:"); String input = scanner.nextLine(); int vowelCount = 0, consonantCount = 0; // Convert the string to lowercase to make the checks case-insensitive input = input.toLowerCase(); // Loop through each character in the string for (char c : input.toCharArray()) { if (c >= 'a' && c <= 'z') { // Check if the character is a letter if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') { vowelCount++; } else { consonantCount++; } } } System.out.println("Number of vowels: " + vowelCount); System.out.println("Number of consonants: " + consonantCount); //scanner.close(); } }
Explanation:
1. Input Handling:
2. Character Analysis:
3. Output:
Output
Input:
Enter a string:
Silan Software
Output:
Number of vowels: 5
Number of consonants: 8
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc