Count and display the number of vowels, consonant, uppercase, lowercase character in strings
Answers
Answer:
5 vowels
21 consonants
26 uppercase
26 lowercase
Answer:
import java.util.*;
public class Main
{
static boolean isVowel(char ch){
switch (ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
return true;
//break;
default:
return false;
}
}
static boolean isConsonant(char c){
if(isVowel(c)){
return false;
}
return true;
}
public static void main(String[] args) {
int v = 0 , c = 0 , d = 0 , uc = 0 , lc = 0 , oth = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string:");
String s = sc.nextLine();
for(int i = 0; i < s.length(); i++){
if(Character.isDigit(s.charAt(i))){
d++;
}
else if(Character.isUpperCase(s.charAt(i))){
uc++;
}
else if(Character.isLowerCase(s.charAt(i))){
lc++;
}
else if(isVowel(s.charAt(i))){
v++;
}
else if(isConsonant(s.charAt(i))){
c++;
}
else{
oth++;
}
}
System.out.println("Vowel count: " + v);
System.out.println("Consonant count: " + c);
System.out.println("Digit count: " + d);
System.out.println("Lower case count: " + lc);
System.out.println("Upper case count: " + uc);
System.out.println("Special Characters: " + oth);
}
}
Explanation: