import java.util.Scanner;
public class JavaProgramToCountVowelsAndConsonants {
public static void main(String[] args) {
// Declare a variables
String str = null;
int vCount = 0, cCount = 0;
Scanner sc = new Scanner(System.in);;
// Accept any string from user
System.out.print("Enter any String: ");
str = sc.nextLine();
// Convert String to lowercase letter
str = str.toLowerCase();
StringBuilder b = new StringBuilder();
StringBuilder c = new StringBuilder();
// For loop to iterate String
for (int i = 0; i < str.length(); i++) {
// Check a character is vowel or not
if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o'
|| str.charAt(i) == 'u') {
// Increment vowel count by one
vCount++;
// Display each vowel
//System.out.println(str.charAt(i));
b.append(str.charAt(i));
} // Check if character is a consonant or not
else if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z') {
// Increment consonant count by one
cCount++;
// Display each consonant
//System.out.println(str.charAt(i));
c.append(str.charAt(i));
}
}
System.out.println("No of vowels given in String: " + vCount + "\t" + b);
System.out.println("No of consonant given in String: " + cCount + "\t" + c);
}
}
No comments:
Post a Comment