A sentence is
terminated by either “ . ” , “ ! ” or “ ? ” followed by a space. Input a piece
of text consisting of sentences. Assume that there will be a maximum of 10
sentences in a block.
Write a program
to:
(i)Obtain the length of the sentence
(measured in words) and the frequency of vowels in each sentence.
(ii)Generate the
output as shown below using the given data
INPUT HELLO! HOW ARE YOU? HOPE
EVERYTHING IS FINE. BEST OF LUCK.
OUTPUT Sentence No. of Vowels No. of words
Sentence No. of vowels No. of words
1 2
1
2 5 3
3 8
4
4 3
3
Sentence No.
of words/vowels
1 VVVVVV
1 WWW
2 VVVVVVVVVVVVVVV
2 WWWWWWWWWWWW
3 VVVVVVVVVVVVVVVVVVVVVVVV
3 WWWWWWWWWWWWWWW
4 VVVVVVVVV
4 WWWWWWWWWWWW
Scale
used 1:3
Solution:-
import java.util.*;
class graph
{
static String str1,str2,str3[];
int number,i;
StringTokenizer stk;
void takeSentence()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the
sentence:");
str1=sc.nextLine();
stk=new StringTokenizer(str1,"!?.,");
number=stk.countTokens();
str3=new String[number];
i=0;
while(stk.hasMoreTokens())
{
str3[i++]=stk.nextToken();
}
System.out.println("\nSentence
No No. of vowels No. of words\n");
for(i=0;i< number;i++)
{
str1=str3[i];
System.out.print((i+1)+" ");
System.out.print(vowel(str1)+
" ");
System.out.println(word(str1)+
" ");
}
}
static
int vowel(String s)//calculate no. of vowels.
{
int i,v=0,len;
len=s.length();
s=s.toUpperCase();
for(i=0;i< len;i++)
{
if(s.charAt(i)=='A'||s.charAt(i)=='E'||s.charAt(i)=='I'||s.charAt(i)=='O'||s.charAt(i)=='U')
v++;
}
return v;
}
static
int word(String s)//calculate no. of words.
{
int i,v=1,len;
len=s.length();
s=s.toUpperCase();
for(i=0;i< len;i++)
{
if(s.charAt(i)==' ')
v++;
}
return v;
}
void
generategraph()//generates graph.
{
for
(int i=0;i<str3.length;i++)
{
System.out.print(i+1+"\t\t\t\t\t\t");
for(int
j=1;j<=vowel(str3[i])*3;j++)
{
System.out.print("V");
}
System.out.println();
System.out.print(i+1+"\t\t\t\t\t\t");
for
(int j=1;j<=word(str3[i])*3;j++)
{
System.out.print("W");
}
System.out.println();
}
}
public static void main(String args[])
{
graph obj=new graph();
obj.takeSentence();
System.out.println("Sentence\t\t\t\tNo.
of words/vowels");
obj.generategraph();
System.out.println("\n Scale used
1:3");
}
}
No comments:
Post a Comment