No Of Consonants In A String
Write a program to print the number of consonants in a string.
/* WAP to print the number of consonants in a string. */
import java.io.*;
class StringQ03
{
public static void main(String arg[])throws IOException
{
int i, l, cons;
String str;
char ch;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a string : ");
str=br.readLine();
l = str.length();
// To count no of vowels
cons = 0;
for(i=0;i < l;i++){
ch = str.charAt(i);
//First check if the character is a letter
if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')){
//Now check if it's not a vowel i.e. character is a consonant
if(ch != 'a' && ch != 'e' && ch != 'i' && ch != 'o' && ch != 'u' &&
ch != 'A' && ch != 'E' && ch != 'I' && ch != 'O' && ch != 'U')
cons++;
}
}
System.out.println("No of Consonants: "+cons);
}//end of main
}//end of class
Have something to say? Log in to comment on this post.
0 comments