Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: ISC COMPUTER SCIENCE PRACTICAL 2005 ~ WORD SORT on Length

Saturday, August 2, 2014

ISC COMPUTER SCIENCE PRACTICAL 2005 ~ WORD SORT on Length

ISC 2005 Computer Science practical solved paper

Write a program which takes a string (maximum 80 characters) terminated by a full stop.
The words in this string are assumed to be separated by one or more blanks.
Arrange the words of the input string in descending order of their lenghts.
Same length words should be sorted alphabetically.
Each word must start with an uppercase letter and the sentence should be terminated by a full stop.

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

SAMPLE DATA:
INPUT:
This is human resource department.

OUTPUT:
Department Resource Human This Is.

INPUT:
To handle yourself use your head and to handle others use your heart.

OUTPUT:
Yourself Handle Handle Others Heart Head Your Your And Use Use To To. 


import java.util.*;
import java.io.*;
public class LengthSort {
    public static void main(String[] args) throws IOException
        {
        Sorting fc = new Sorting();
        String msg;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));   
        System.out.println(" Enter the sentence:");
        msg = br.readLine();
        if ( msg.charAt(msg.length()-1) != '.')
            System.out.println("Incorrect format, still we are sorting for you!");
        fc.setMsg(msg);
        System.out.println("OUTPUT:");
        fc.totalWords();
        fc.wordExtraction();
        fc.output();
        System.out.println("");
        }
    }   
class Sorting {
    Sorting()
        {
        msg = " ";
        words = 0;
        }
    public void setMsg(String s) {   
        msg = s;
        len = s.length();
        }
    public void totalWords() {
        for ( int i =0; i<len ; i++)
            if(msg.charAt(i) == ' ')
                words++;
        wrdsCollection = new String[words+1];   
        for(int i = 0; i< words+1 ; i++ )
            wrdsCollection[i]= " ";
        }   
    public void wordExtraction() { 
        int j=0;
        for ( int i =0; i<len ; i++) {
            while(msg.charAt(i) != ' ')
                {
                if(msg.charAt(i) == '.')
                    { i++;
                    if(i>len-1)
                        break;
                    continue;
                    }
                wrdsCollection[j] +=msg.charAt(i);
                i++;
                if(i>len-1)
                    break;
                if((msg.charAt(i) == ' ')|| i >len-1)
                    break;
                }
            if(i>len-1)
                break;   
            j++;
            }
        }   
    public void output() {
    System.out.println("Rearranged Sentence:");
        for(int j = 0; j < wrdsCollection.length; j++) {
            for(int i = j+1; i < wrdsCollection.length; i++) {
                if(wrdsCollection[i].length() >= wrdsCollection[j].length()  ) {
                    if ( wrdsCollection[i].length() == wrdsCollection[j].length()) {
                        if(wrdsCollection[i].compareTo(wrdsCollection[j]) > 0) {
                            String temp = wrdsCollection[j];
                            wrdsCollection[j] = wrdsCollection[i];
                            wrdsCollection[i] = temp;
                            }
                        }
                    String temp = wrdsCollection[j];
                    wrdsCollection[j] = wrdsCollection[i];
                    wrdsCollection[i] = temp;
                    }
                }
            System.out.print(wrdsCollection[j]+" ");
            }   
        }   
    private String[] wrdsCollection;
    private int[] freCount;           
    private int len;
    private String msg;
    private int words;
    private boolean flag;   
    }

No comments:

Post a Comment