Program to count vowel or consonant in string in java.
package program;
import java.util.Scanner;
public class code
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("enter string=");
String s = sc.nextLine();
int v=0,c=0;
for(int i=0; i<s.length(); i++){
if(check(s.charAt(i)))
v++;
else
c++;
}
System.out.println("vowels are="+v);
System.out.println("consonent are="+c);
}
public static boolean check(char c){
if(c=='a' || c=='A') return true;
if(c=='e' || c=='E') return true;
if(c=='i' || c=='I') return true;
if(c=='o' || c=='O') return true;
if(c=='u' || c=='U') return true;
return false;
}
}
Comments
Post a Comment