Write a program to take a String as input then display the words which starts with vowel and also display how many such words found.
For example:
Enter a String
HELLO world elePhaNt rUsh imagine
Words which start from vowel
elePhaNt imagine
No. of such words: 2
import java.util.*;
class App {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a String: ");
String inputWords[] = in.nextLine().split(" ");
int n = 0;
System.out.println("Words which start from vowel");
for (String word : inputWords) {
if (word.toLowerCase().charAt(0) == 'a' || word.toLowerCase().charAt(0) == 'e'
|| word.toLowerCase().charAt(0) == 'i' || word.toLowerCase().charAt(0) == 'o'
|| word.toLowerCase().charAt(0) == 'u') {
System.out.print(word + " ");
n++;
}
}
System.out.println("\nNo. of such words: " + n);
in.close();
}
}
Comments
Leave a comment