check vowels in string java code example
Example 1: count vowels in a string java
int count = 0;
String sentence = "My name is DEATHVADER, Hello World!!! XD";
char[] vowels = {'a', 'e', 'i', 'o', 'u'};
for (char vowel : vowels)
for (char letter : sentence.toLowerCase().toCharArray())
if (letter == vowel) count++;
System.out.println("Number of vowels in the given sentence is " + count);
Example 2: string vowels program in java
import java.util.Scanner;
import java.util.*;
class Test{
public static boolean isVowel(char ch){
if(ch == 'A' || ch == 'a' || ch == 'E' || ch == 'e' || ch == 'i' || ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U'){
return true;
}
return false;
}
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
List<String> arr = new ArrayList<String>();
String str = scan.nextLine();
String words[] = str.split("\\s");
for(String k: words){
boolean flag = false;
boolean f1 = true;
boolean f2 = true;
if(k.length()<=2){
continue;
}
for(int i = 0 ; i < k.length() ; i = i + 2){
if(isVowel(k.charAt(i))==false){
f1 = false;
break;
}
}
for(int i = 1 ; i < k.length() ; i = i + 2){
if(isVowel(k.charAt(i))==false){
f2 = false;
break;
}
}
if(f1 == true || f2 == true){
flag = true;
}
if(flag == true){
arr.add(k);
}
}
if(arr.size()==0){
System.out.print(-1);
}
else{
for(int i = 0 ; i < arr.size() ; i++)
{
System.out.print(arr.get(i) + " ");
}
}
scan.close();
}
}