How we can make a program in which we have to accept a sentence from a user and print those words which starts with a vowel…
/**
* The class Vowel_Word inputs a sentence and prints words which begin with a vowel
* @Program Type : BlueJ Program - Java
*/
import
java.io.*;
class
Vowel_Word
{
public
static
void
main(String args[])
throws
IOException
{
BufferedReader br=
new
BufferedReader (
new
InputStreamReader(System.in));
System.out.print(
"Enter any sentence: "
);
String s=br.readLine();
s = s+
" "
;
int
l=s.length();
int
pos=
0
;
char
ch1, ch2;
String w;
for
(
int
i=
0
; i<l; i++)
{
ch1 = s.charAt(i);
if
(ch1 ==
' '
)
{
w = s.substring(pos,i);
// extracting words one by one
ch2 = w.charAt(
0
);
if
(ch2==
'A'
|| ch2==
'E'
|| ch2==
'I'
|| ch2==
'O'
|| ch2==
'U'
||
ch2==
'a'
|| ch2==
'e'
|| ch2==
'i'
|| ch2==
'o'
|| ch2==
'u'
)
{
System.out.println(w);
}
pos = i+
1
;
}
}
}
}
String input = "In that period I was a student in the bad college unfortunately";
ReplyDeleteString[] text = input.split(" ");
for(String regword: text){
if(regword.matches("^[Aa,Ii,Oo,Uu,Ee].*"))
System.out.println(regword+" matches");
}
output: In matches
I matches
a matches
in matches
unfortunately matches
It helped me a lot.
ReplyDeleteThanks👍
Good
ReplyDeletelucid answer
ReplyDeletePls read my stories too in my Blog 😰😰
ReplyDelete