Problem Statement
Write a Java program to count both vowels and consonants in a given string
Conditions
Only alphabets should be considered for counting
Input
Hello World!
Output
Vowels count - 3
Consonants count - 7
Explanation
The vowels in the given string are e,o and o and so the vowels count is 3. Likewise, the consonants in the given string are h,l,l,w,r,l and d and so the count is 7.
code:
import java.util.Scanner;
public class Solution
{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String input = s.nextLine();
s.close();
input = input.toLowerCase();
int vowelCount = 0, consonantCount = 0;
for(int i=0;i<input.length();i++){
if(input.charAt(i)>='a' && input.charAt(i)<='z'){
if(input.charAt(i)=='a' || input.charAt(i)=='e' || input.charAt(i)=='i' || input.charAt(i)=='o' || input.charAt(i)=='u')
vowelCount += 1;
else
consonantCount += 1;
}
}
System.out.println("Vowels count - "+vowelCount);
System.out.println("Consonant Count - "+consonantCount);
}
}
Comments
Post a Comment