Sunday, April 29, 2018

Java Program to count the number of vowels in a sentence

package javapkg;

public class FIndVowels {


public static void main(String[] args) {

String s1="Sun Rises in East";
String[] words = s1.split(" ");
for(int i=0; i<=words.length-1; i++)
{
s1=s1.toLowerCase();
int vowelCount=0;
char[] charArray = words[i].toCharArray();
for(int j=0; j<charArray.length; j++)
{
if(charArray[j]=='a'||
charArray[j]=='e'||charArray[j]=='i'||charArray[j]=='o'||
charArray[j]=='u')
{
vowelCount++;
}

}
System.out.print("No. of vowels in "+words[i]+" is: "+vowelCount+" ");
System.out.println(); // To break the line

}

}

}

Output:
No. of vowels in Sun is: 1 
No. of vowels in Rises is: 2 
No. of vowels in in is: 1 
No. of vowels in East is: 1 





                                                                   Java split() method with examples
                                                                   Java toCharArray() method with examples


No comments:

Post a Comment