Input a paragraph containing 'n' number of sentences where (1<=n<=4). The words are to be separated with single blank space and are in upper case. A sentence may be terminated either with a full stop (.) or question mark (?).
Perform the following:
- Enter the number of sentences, if it exceeds the limit show a message.
- Find the number of words in the paragraph
- Display the words in ascending order with frequency.
Example 1
INPUT: Enter number of sentences: 1
Enter sentences:
TO BE OR NOT TO BE.
OUTPUT:
Total number of words: 6
WORD FREQUENCY
OR 1
NOT 1
TO 2
BE 2
Example 2
INPUT: Enter number of sentences: 3
Enter sentences:
THIS IS A STRING PROGRAM. IS THIS EASY? YES, IT IS.
OUTPUT:
Total number of words: 11
WORD FREQUENCY
A 1
STRING 1
PROGRAM 1
EASY 1
YES 1
IT 1
THIS 2
IS 3
import java.io.*;
class ISCprac2010q03{
public static void main(String args[])
throws IOException{
BufferedReader br=new BufferedReader(
new InputStreamReader(System.in));
int nos;
System.out.print("Enter number of sentences : ");
nos=Integer.parseInt(br.readLine());
if(nos<1 || nos>4)
System.out.println("\nInvalid Entry");
else{
String s;
int i,l,words=0,p=0,x=0,y=0,j,k,freq;
System.out.println("Enter a paragraph containing "+
nos+" sentences: ");
s=br.readLine().toUpperCase();
l=s.length();
String []w=new String[l];
for(i=0;i< l;i++){
char ch=s.charAt(i);
if(ch==' ' || ch=='!' || ch=='.' || ch=='?'){
words++;
if(s.charAt(i-1)==',')
w[x++]=s.substring(p,i-1);
else
w[x++]=s.substring(p,i);
p=i+1;
}
}
int f[]=new int[x];
for(i=0;i< x;i++)
{
freq=1;
for(j=i+1;jf[j]){
int t=f[i];
f[i]=f[j];
f[j]=t;
String r=w[i];
w[i]=w[j];
w[j]=r;
}
}
}
for(i=0;i< y;i++){
System.out.println(w[i]+" "+f[i]);
}
}
}
}
Have something to say? Log in to comment on this post.
0 comments