Saturday, March 21, 2020

Program On Finding Number of Words Beginning and Ending With Vowel


For Details of the program: CLICK HERE

Variable Description

Type
Name
Purpose
Scope
int
x
Used as array index
Within all functions


for words starting



and ending with



vowel

int
y
Used as array index
Within all functions


for words not



starting and ending



with vowel

BufferedReader
br
Used for user input
Within void



takeString ()



function
String
vowel []
Stores words
Within all functions


starting and ending



with vowel

String
other []
Stores words not
Within all functions


starting and ending



with vowel

char
ch
Used to store single
Within void


characters extracted
takeString ()


from individual
function


words

String
str
Stores the original
Used within


sentence
functions, takeString



(), breakString ()



and show ()



functions
String
s
To store individual
Within function


words from the
breakString ()


sentence



Algorithm

Step 1: All the variables are created


Step 2: Take the sentence from the user and check for validity. If it is a valid string then proceed further otherwise stop.

Step 3: Break the sentence into words and the words with vowels at both the ends are stored in array ‘vowel []’ and the rest are stored in array ‘other []’.

Step 4: The array containing the vowels are sorted

Step 5: Extract the words from array vowel [] and combine them and finally the words from array ‘other []’ are appended to form the new desired sentence

Step 6: Display

import java.io.*;
class CPrime

{
int x=0,y=0,i;
char ch;
String str,s;
String vowel[],other[];
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
void takeString()throws Exception
{

System.out.println("Enter the String (in upper case):");
 str=br.readLine().toUpperCase(); ch=str.charAt(str.length()-1);

if((ch>=65 && ch<=90) || ch=='.' ||ch=='?' ||ch=='!')
{}
else
{
System.out.println("Wrong Input:");
return;
}
System.out.println(" Original String:"+str);
if( ch=='.' ||ch=='?' ||ch=='!')
str=str.substring(0,str.length()-1);
vowel=new String[str.length()];
other=new String[str.length()];
}

void breakString()
{
while(true)
{
i=str.indexOf(' ');
if(i==-1)
break;
s=str.substring(0,i).trim();
str=str.substring(i+1).trim();
if (sVowel(s))
vowel[x++]=s;
else

other[y++]=s;

}
if (sVowel(str))
vowel[x++]=str;
else
other[y++]=str;
}
boolean sVowel(String s)
{
int flag=0;
switch(s.charAt(0))
{
case 'A':
case 'E':
case 'I':
case 'O':

case 'U':
flag+=1;
}
switch(s.charAt(s.length()-1))
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
flag+=1;
}
if(flag==2)
return true;
else
return false;
}
 void sort()
{
int i,j;
String t;
for(i=0;i< x-1;i++)
{

for(j=i+1;j< x;j++)
{
if(vowel[i].compareTo(vowel[j])>0)
{
t=vowel[i];
vowel[i]=vowel[j];
vowel[j]=t;
}
}
}
}
void show()

{

System.out.println("\n Number of words beginning and ending with vowel:"+x);
str="";
for(i=0;i< x;i++)
str=str+ " " + vowel[i];
for(i=0;i< y;i++)
str=str+ " " + other[i];
if (ch=='.' ||ch=='?' ||ch=='!')
str=str.trim()+ch;
System.out.print(" Modified String:"+str);
}
public static void main(String args[]) throws Exception
{
CPrime ob=new CPrime();
ob.takeString(); 
ob.breakString(); 
ob.sort();
ob.show(); 
} 

}

Back To 2016 Computer Practical paper: CLICK HERE

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner