Question
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:
Find the number of words beginning and ending with a vowel.
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
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class VowelCheck
{
public static boolean WordCheck(String w)
{
int len1 = w.length();
char first = w.charAt(0);
char last = w.charAt(len1 - 1);
if(isVowel(w.charAt(0))==true && isVowel(w.charAt(len1-1))==true)
{
return true;
}
else
{
return false;
}
}
public static boolean isVowel(char ch)
{
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
{
return true;
}
else
{
return false;
}
}
public static void main(String args[])
{
int count=0,i=0,len=0;
char last=' ',ch=' ';
String sen="",wd="",vowels="",nonVowels="";
Scanner sc=new Scanner(System.in);
System.out.print("Sentence: ");
sen = sc.nextLine();
sen = sen.trim();
sen = sen.toUpperCase();
len = sen.length();
last = sen.charAt(len - 1);
if(last != '.' && last != '?' && last != '!')
{
System.out.println("INVALID INPUT");
return;
}
for(i = 0; i < len; i++)
{
ch = sen.charAt(i);
if(ch==' '||ch=='.'||ch=='?'||ch==','||ch=='!')
{
if(wd.length() > 0 && WordCheck(wd)==true)
{
count++;
vowels += wd + " ";
}
else
{
nonVowels+= wd + " ";
}
wd = "";
}
else
{
wd += ch;
}
}
System.out.println("NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = " + count);
System.out.println(vowels + nonVowels);
}
}