Write a Java method to count all vowels in a string.
import java.util.Scanner;
public class FindVowels{
public static int getVowels(String word){
String [] vowels={"a","A","e","E","i","I","o","O","u","U"};
int count=0;
for(int i=0;i<vowels.length;i++){
String a=vowels[i];
for(int j=0;j<word.length();j++){
String b=String.valueOf(word.charAt(j));
if(a.equals(b)){
count++;
}
}
}
return count;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string");
String word=sc.next();
System.out.println("There are "+getVowels(word)+" vowels in String: "+word );
sc.close();
}
}
Output:
Comments
Post a Comment