Given a sentence ending with ‘.’ or ‘?’ or ‘!’ the task is to only print the words that start with vowels.
Example: The office computers are not connected to internet. The program should print the words that start with vowels. That is “office“, “are” and “internet“.
Java
import java.util.*;
class WordVowelS
{
public static void main(String args[])
{
String str="",wrd="";
int l=0;
char ch=' ',sCh=' ';
Scanner sc=new Scanner(System.in);
System.out.println("Enter a sentence ending with '.' or '?' or '!'");
str=sc.nextLine();
str=str.trim();
l=str.length();
ch=str.charAt(l-1);
if(!(ch=='.'||ch=='?'||ch=='!'))
{
System.out.println("Error! Sentence must be terminated by punctuation");
System.exit(1);
}
System.out.println("Words starting with vowels:-");
for(int i=0;i<l;i++)
{
ch=str.charAt(i);
if(ch==' '||ch=='.'||ch=='?'||ch=='!')
{
sCh=wrd.charAt(0);
if(sCh=='A'||sCh=='E'||sCh=='I'||sCh=='O'||sCh=='U'||sCh=='a'||sCh=='e'||sCh=='i'||sCh=='o'||sCh=='u')
{
System.out.println(wrd);
}
wrd="";
}
else
{
wrd=wrd+ch;
}
}
}
}Java