This program is selected from ISC 2016 Computer Science or Class 12 practical paper.
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 I UPPER CASE.
Perform the following tasks:
Example 1:
INPUT:ANAMIKA AND SUSAN ARE NEVER GOING TO QUARREL ANYMORE.
OUTPUT:NUMBER OF WORDS BEGINNING AND ENDING WITH VOWEL = 3
ANAMIKA ARE ANYMORE AND SUSAN NEVER GOING TO QUARREL.
import java.util.*; public class BegEndVowel { public static void main(String []args) { Scanner sc=new Scanner(System.in); BegEndVowel bev=new BegEndVowel(); System.out.print("Enter Your String : "); String str=sc.nextLine(); int len=str.length(); char ch=str.charAt(len-1); String res=""; String temp=""; if(ch=='.' || ch=='?' || ch=='!') { String []words=str.substring(0,len-1).split(" "); int c=0; for(int i=0;i<words.length;i++) { if(words[i].length()>0) { if(bev.checkword(words[i])) { c++; res += words[i] + " "; } else { temp += words[i] + " "; } } } System.out.println("NUMBER OF WORDS BEGINNING AND ENDING WITH VOWEL = " + c); res = (res + temp).trim(); System.out.println(res); } else { System.out.println("INVALID INPUT"); } } boolean checkword(String s) { int len=s.length(); char ch1=s.charAt(0); char ch2=s.charAt(len-1); if((ch1=='A' || ch1=='E' || ch1=='I' || ch1=='O' || ch1=='U') && (ch2=='A' || ch2=='E' || ch2=='I' || ch2=='O' || ch2=='U')) { return true; } else { return false; } } }
Example 1
Enter Your String : ANAMIKA AND SUSAN ARE NEVER GOING TO QUARREL ANYMORE.
NUMBER OF WORDS BEGINNING AND ENDING WITH VOWEL = 3
ANAMIKA ARE ANYMORE AND SUSAN NEVER GOING TO QUARREL
Example 2
Enter Your String : YOU MUST AIM TO BE A BETTER PERSON TOMORROW THAN YOU ARE TODAY.
NUMBER OF WORDS BEGINNING AND ENDING WITH VOWEL = 2
A ARE YOU MUST AIM TO BE BETTER PERSON TOMORROW THAN YOU TODAY
Example 3
Enter Your String : LOOK BEFORE YOU LEAP.
NUMBER OF WORDS BEGINNING AND ENDING WITH VOWEL = 0
LOOK BEFORE YOU LEAP
Example 4
Enter Your String : HOW ARE YOU@
INVALID INPUT