Sunday, February 19, 2012

Sort words of a sentence

Read a single sentence which terminates with a full stop(.). The words are to be separated with a single blank space and are in lower case. Arrange the words contained in the sentence according to the length of the words in ascending order. If two words are of the same length then the word occurring first in the input sentence should come first. For both, input and output the sentence must begin in upper case.

Test your program for the following data and some random data.

INPUT : The lines are printed in reverse order.

OUTPUT : In the are lines order printed reverse.

INPUT : Print the sentence in ascending order.

OUTPUT : In the print order sentence ascending.


import java.io.*;

import java.util.*;

class sortlength

{

public static void main(String args[])throws IOException

{

String t,a;

int i, k,j=0, x=0, l, l1, l2, l3;

BufferedReader ob=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter the sentence");

a=ob.readLine();

a=a.toLowerCase();

a=a.substring(0,a.length()-1);

StringTokenizer str=new StringTokenizer(a);

l=str.countTokens();

String z[ ]=new String[l];

for(i=0;i

z[j++]=str.nextToken( );

for(i=0;i

{

for(k=0;k<(l-1);k++)

{ l2=z[k].length();

l3=z[k+1].length();

if(l2>l3)

{ t=z[k];

z[k]=z[k+1];

z[k+1]=t;

}

}

}

System.out.print((char)(z[0].charAt(0)-32)+z[0].substring(1)+" ");

for(i=1;i

System.out.print(z[i]+" ");

System.out.print(z[j-1]+".");

}

}

3 comments:

Anonymous said...

sir i m Abhinav from UP Lucknow if u have any sort of information about the pracTical of ISC please help my on my contact number +918896238595 aur either post your number i m very glad to hear u soon

Unknown said...

contact me on my email kumardks@gmail.com

nancy069 said...

sir,plz give me some information regarding isc practical..the programs which can come.