Write a program to accept a sentence which may be terminated by either ‘.’, ‘?’ or ‘!’ only. The words may be separated by more than one blank space and are in uppercase.
Perform the following tasks:
(a) Find the number of words beginning and ending with a vowel.
(b) Place the words which begin and end with a vowel at the beginning, followed by the remaining words as they occur in the sentence.
Test your program with the sample data and some random data:
Example 1
INPUT:
ANAMIKA AND SUSAN ARE NEVER GOING TO QUARREL ANYMORE.
OUTPUT:
NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 3
ANAMIKA ARE ANYMORE AND SUSAN NEVER GOING TO QUARREL
Example 2
INPUT:
YOU MUST AIM TO BE A BETTER PERSON TOMORROW THAN YOU ARE TODAY.
OUTPUT:
NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 2
A ARE YOU MUST AIM TO BE BETTER PERSON TOMORROW THAN YOU TODAY
Example 3
INPUT:
LOOK BEFORE YOU LEAP.
OUTPUT:
NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 0
LOOK BEFORE YOU LEAP
Example 4
INPUT:
HOW ARE YOU@
OUTPUT:
INVALID INPUT
import java.util.Scanner;
import java.util.StringTokenizer;
class Vowels{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Enter the sentence: ");
String s = in.nextLine().toUpperCase();
s = s.trim();
char last = s.charAt(s.length() - 1);
if(".?!".indexOf(last) == -1){
System.out.println("INVALID INPUT");
return;
}
StringTokenizer st = new StringTokenizer(s, " .?!");
int count = st.countTokens();
String v = new String();
String nv = new String();
int f = 0;
for(int i = 1; i <= count; i++){
String word = st.nextToken();
char first = word.charAt(0);
last = word.charAt(word.length() - 1);
int a = "AEIOU".indexOf(first);
int b = "AEIOU".indexOf(last);
if(a >= 0 && b >= 0){
v += word + " ";
f++;
}
else
nv += word + " ";
}
String t = v + nv;
System.out.println("NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = " + f);
System.out.println(t);
}
}