Write a program to accept a single string from the user and count the number of voewels & consonants in the given string (USING BLUE J) ANSWER FAST NO SPAM❌
System.out.println("Number of vowels: " + vowelCount);
System.out.println("Number of consonants: " + consonantCount);
scanner.close();
}
}
Explanation:
This program uses a for loop to iterate through each character of the input string. It checks if each character is a letter, and then determines if it is a vowel or consonant. The counts are then printed at the end. Note that this program considers only English alphabets and ignores case.
Answers & Comments
Answer:
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 inputString = scanner.nextLine().toLowerCase(); // Convert the input to lowercase for case-insensitivity
int vowelCount = 0;
int consonantCount = 0;
for (int i = 0; i < inputString.length(); i++) {
char ch = inputString.charAt(i);
// Check if the character is an alphabet
if (Character.isLetter(ch)) {
// Check if the alphabet is a vowel
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
vowelCount++;
} else {
consonantCount++;
}
}
}
System.out.println("Number of vowels: " + vowelCount);
System.out.println("Number of consonants: " + consonantCount);
scanner.close();
}
}
Explanation:
This program uses a for loop to iterate through each character of the input string. It checks if each character is a letter, and then determines if it is a vowel or consonant. The counts are then printed at the end. Note that this program considers only English alphabets and ignores case.