Write A Java Program To Count The Number of Words in A String?
Write A Java Program To Count The Number of Words in A String?
net/
We've used regular expression \\s that finds all white space characters (tabs, spaces, new line
character, etc.) in the string. Then, we replace it with "" (empty string literal).
line = line.toLowerCase();
for(int i = 0; i < line.length(); ++i)
{
char ch = line.charAt(i);
if(ch == 'a' || ch == 'e' || ch == 'i'
|| ch == 'o' || ch == 'u') {
++vowels;
}
else if((ch >= 'a'&& ch <= 'z')) {
++consonants;
}
else if( ch >= '0' && ch <= '9')
{
++digits;
}
else if (ch ==' ')
{
++spaces;
}
}