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 UPPER CASE.
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, following 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
import java.io.*;
public class ISC2015Q1 {
public static void main(String[] args) throws IOException{
String Sentence;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the sentence:");
Sentence=br.readLine();
while(Sentence.charAt(Sentence.length()-1)!= '?' && Sentence.charAt(Sentence.length()-1)!= '.' && Sentence.charAt(Sentence.length()-1)!= '!' ) {
System.out.println("INVALID INPUT:,TRY
AGAIN");
Sentence=br.readLine();
}
Vowel obj = new Vowel(Sentence);
obj.wordCount();
obj.wordExtractor();
System.out.println("OUTPUT:");
obj.vowelEndingWordCount();
System.out.println();
obj.display();
br.close();
}
}
class Vowel{
public void display() {
for(int i=0;i<word_counter;i++)
if(position_tag[i]==1)
System.out.print(word_Storage[i]+"
");
for(int j=0;j<word_counter;j++)
if(position_tag[j]==0)
System.out.print(word_Storage[j]+"
");
}
public void vowelEndingWordCount() {
for(int i=0;i<word_counter;i++) {
position_tag[i]=0;
flag = false;
for(int j=0;j<5;j++)
if(word_Storage[i].charAt(0)==vowels[j])
for(int k=0;k<5;k++)
if(word_Storage[i].charAt(word_Storage[i].length()-1)==vowels[k])
flag = true;
if(flag==true) {
number_beg_end_with_vowel++;
position_tag[i]=1;
}
}
System.out.println("NUMBER OF WORDS BEGINING AND ENDING WITH A VOWEL:"+number_beg_end_with_vowel);
}
public void wordExtractor() {
int k = 0;
for(int i=0;i<msg.length();i++) {
word_Storage[k]="";
while(msg.charAt(i) != ' ' && msg.charAt(i) != '?' && msg.charAt(i) != '.' && msg.charAt(i) != '!') {
word_Storage[k] +=msg.charAt(i);
i++;
}
k++;
}
}
public void wordCount() {
for(int i=0;i<msg.length();i++)
if(msg.charAt(i)== ' ')
word_counter++;
word_counter +=1;
word_Storage = new String[word_counter];
position_tag = new int[word_counter];
}
Vowel (String Sentence) {
msg = Sentence;
number_beg_end_with_vowel = 0;
word_counter = 0;
}
private boolean flag;
private char[] vowels= {'A','E','I','O','U'};
private int word_counter;
private int number_beg_end_with_vowel;
private int[] position_tag;
private String msg;
private String[] word_Storage;
}
No comments:
Post a Comment