This is a BlueJ program to display the words of a sentence with vowel at the end. User will enter any sentence and the words in the sentence which ends with a vowel will be displayed.
This program can be done with the help of indexOf() and substring () functions of String class as well as without using those two functions.
Here is the BlueJ program using indexOf () and substring () functions.
import java.io.*;
class String1
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void show() throws Exception
{
String str1,s1;
int i;
System.out.print("Enter the sentence:");
str1=br.readLine();
System.out.println("words in the sentence which ends with a vowel are");
while(true)
{
i=str1.indexOf(" ");
if(i<0 p=""> break;
s1=str1.substring(0,i);
str1=str1.substring(i+1);
if(vowelChk(s1))
System.out.println(s1);
}
if(vowelChk(str1))
System.out.print(" "+str1);
}
private boolean vowelChk(String s)
{
boolean bool=false;
s=s.toUpperCase();
switch(s.charAt(s.length()-1))
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
bool=true;
}
return bool;
}
public static void main(String args[])throws Exception
{
String1 ob=new String1();
ob.show();
}
}
You can also do the program with appending a space at the end of the sentence.
import java.io.*;
class String1
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void show() throws Exception
{
String str1,s1;
int i;
System.out.print("Enter the sentence:::");
str1=br.readLine().trim()+" ";
System.out.println("words in the sentence which ends with a vowel are");
while(true)
{
i=str1.indexOf(" ");
if(i<0 p=""> break;
s1=str1.substring(0,i);
str1=str1.substring(i+1);
if(vowelChk(s1))
System.out.println(s1);
}
}
private boolean vowelChk(String s)
{
boolean bool=false;
s=s.toUpperCase();
switch(s.charAt(s.length()-1))
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
bool=true;
}
return bool;
}
public static void main(String args[])throws Exception
{
String1 ob=new String1();
ob.show();
}
}
Here is the program without using indexOf() and substring () function
import java.io.*;
class String1
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void show() throws Exception
{
String str1,s1="";
int i,len;
char ch;
System.out.print("Enter the sentence:");
str1=br.readLine().trim()+" ";
System.out.print("words in the sentence which ends with a vowel are");
len=str1.length();
for(i=0;i< len;i++)
{
ch=str1.charAt(i);
if (ch!=' ')
s1=s1+ch;
else
{
if(vowelChk(s1))
System.out.println(s1);
s1="";
}
}
}
private boolean vowelChk(String s)
{
boolean bool=false;
s=s.toUpperCase();
switch(s.charAt(s.length()-1))
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
bool=true;
}
return bool;
}
public static void main(String args[])throws Exception
{
String1 ob=new String1();
ob.show();
}
}
0>0>
<0 p=""><0 p="">
0>0> <0 p=""><0 p="">Related Post: BlueJ Programs on String/Sentence0>0>
All the three programs are perfect. Time complexity of the third program is higher than the first two programs as in the third program the loop executes more times than the first two programs. ICSE and ISC students can solve the program using any one of the techniques.
I want feed back from students specially on using indexOf() and substring () functions. Computer teacher of a famous Burdwan school deducts marks if any student of his school uses indexOf() and substring () function. I came to know about the matter from one of the students of the school. I don't know
whether this is due to lack of knowledge or some other reasson. The same teacher said earlier that 'public static void main (String args[])' is not the main function of java program.
No comments:
Post a Comment