Wednesday 1 February 2017

Sentence Sorting...

Accept a paragraph Of text cons of sentences that are terminated by either ' . ' (fun stop), '!' (exclamation mark) or a '?' (question mark). Assume that there can be maximum 10 sentences in a paragraph. Write a program to arrange the sentences n increasing order of their number of words.
Example : 
INPUT : Please come and attend the party. Hello! How are you? 
OUTPUT : 
Hello = 1 
How are you = 3
Please come and attend the party = 6 


import java.util.*;
class sentence
{
String str;
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter A Sentence");
str=sc.nextLine();
}
int countword(String S)//counts number of words
{
StringTokenizer stk=new StringTokenizer(S);
return stk.countTokens();
}
void perform()
{
StringTokenizer stk=new StringTokenizer(str,".!?");
int count=stk.countTokens();
String arr1[]=new String[count];
int arr2[]=new int[count];
for(int i=0;i<count;i++)
{
String s=stk.nextToken().trim();
arr1[i]=s;
arr2[i]=countword(s);
}
String temp1;
int temp2;
for(int i=0;i<count;i++)//sorting (Bubble Sort)...
{
for(int j=0;j<count-1;j++)
{
if(arr2[j]>arr2[j+1])
{
temp1=arr1[j];
temp2=arr2[j];
arr1[j]=arr1[j+1];
arr2[j]=arr2[j+1];
arr1[j+1]=temp1;
arr2[j+1]=temp2;
}
}
}
for(int i=0;i<count;i++)//Displaying the sorted arrays
{
System.out.println(arr1[i]+" = "+arr2[i]);
}
}
public static void main(String args[])
{
sentence obj =new sentence();
obj.input();
obj.perform();
}

}



No comments:

Post a Comment

Goldbach Number ISC 2018

Question: A Goldbach number is a positive even integer that can be expressed as the sum of two odd primes. Note: All even integer numbers ...