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 SOLVED QUESTION 1 : STRING DOUBLE SORT

Saturday, March 30, 2019

ISC COMPUTER SCIENCE PRACTICAL 2005 SOLVED QUESTION 1 : STRING DOUBLE SORT

Write a program which takes a string (maximum 80 characters) terminated by a full stop. The words in this string are assumed to be separately by one or more blanks

Arrange the words of the input string in descending order of their lengths. Same length words should be sorted alphabetically. Each word must start with a n uppercase letter and the sentence should be terminated by a full stop.

Test your program from the following dat 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 if Handle Handle Other Heart Head You’re your And Use Use To To.

At first look the program looks quite simple and straight forward. Just input the string,
check its validity, extract the words, convert the first letter of each word to uppercase and sort with respect to length. But if you examine closely you will see that is given that the
"words of  the string can be seperated by one or more blanks".
Secondly if two or more words are of equal length then that set of words are to be
sorted alphabetically.

Coming to the problem of sorting lengthwise I have used Bubble sort and the same
technique has been used to sort alphabetically for words of equal length. See code below.

public void sortWords() {
              String temp;
              int i,j;
              for(i = 0;i<word_counter-1; i++)      
                       for (j = 0; j < word_counter-i-1; j++) 
                         if (word_Storage[j].length() <= word_Storage[j+1].length()) { // sorting length wise
                              temp=word_Storage[j+1];
                            word_Storage[j+1]=word_Storage[j];
                            word_Storage[j]=temp;
                            }
              for(i = 0;i<word_counter-1; i++)      
                       for (j = 0; j < word_counter-i-1; j++)
                             if(word_Storage[j].length() == word_Storage[j+1].length()) { // if lengths are equal sort alphabetically
                         if(word_Storage[j].compareTo(word_Storage[j+1])>0) {
                            temp=word_Storage[j+1];
                                     word_Storage[j+1]=word_Storage[j];
                                     word_Storage[j]=temp;
                            }
                                  }
              }

The real challenge in this question is to extract words since they may be seperated by more that one or one blank.
For example, if the input is "This     is a     test      example."
At first we need to arrange in the following format "This is a test example."
Following this we can count the number of words just by counting the number of spaces
and the words can be extracted easily,
To do this we the method public void deleteExtraSpaces() has been used.

public void deleteExtraSpaces() {
              int counter,start;
              String temp="";
              for(int i=0;i<msg.length();i++) {
                     counter=0;
                     start=i;
                     while(msg.charAt(i)!= ' ' && i<msg.length() && msg.charAt(i)!= '.')
                           i++;
                     counter=i;
                     temp += msg.substring(start,counter+1);
                     counter +=1;
                     if(counter >= msg.length())
                           break;
                     while(msg.charAt(counter)==' ')
                           counter++;
                     i=counter-1;
                     }
              msg=temp;

       }
The above method delete all the extra blanks keeping only one. I recommend it to dry run
and understand the logic. Such a method will be useful to amny others program.
In short the logic is move the counter untill you get the first blank. Mark the start and end positions. Add this subtring to a temporary string now move the counter from the first blank untill the next character appears. Set the counters accordingly. 

Complete code for the program is given below:


import java.io.*;
import java.util.*;
public class ISC2005Q1{
       public static void main(String[] args) throws IOException{
               String Sentence;
               int char_counter=0;
               BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
               do {
               char_counter=0;
               System.out.println("Enter the sentence:");
               Sentence=br.readLine();
               for(int i=0;i<Sentence.length();i++) {
                 if(Sentence.charAt(i)!= ' ')
                        char_counter++;
                           }
               //System.out.println(Sentence.length());
               if( Sentence.charAt(Sentence.length()-1)!= '.' || char_counter > 80)
                  System.out.println("INVALID INPUT:,TRY AGAIN");
               } while(Sentence.charAt(Sentence.length()-1)!= '.' || char_counter >80 );
               SenetenceSort obj = new SenetenceSort(Sentence);
               obj.deleteExtraSpaces();
               obj.wordExtractor();
               obj.sortWords();
               System.out.println("OUTPUT:");
               obj.display();
               br.close();
       }
}
class SenetenceSort{
       public void display() {
              for(int i = 0;i<word_counter;i++) {
                     if(i==word_counter-1)
                           System.out.print(word_Storage[i]+".");
                     else
                           System.out.print(word_Storage[i]+" ");
                     }
       }
       public void sortWords() {
              String temp;
              int i,j;
              for(i = 0;i<word_counter-1; i++)      
                       for (j = 0; j < word_counter-i-1; j++) 
                         if (word_Storage[j].length() <= word_Storage[j+1].length()) { // sorting length wise
                              temp=word_Storage[j+1];
                            word_Storage[j+1]=word_Storage[j];
                            word_Storage[j]=temp;
                            }
              for(i = 0;i<word_counter-1; i++)      
                       for (j = 0; j < word_counter-i-1; j++)
                             if(word_Storage[j].length() == word_Storage[j+1].length()) { // if lengths are equal sort alphabetically
                         if(word_Storage[j].compareTo(word_Storage[j+1])>0) {
                            temp=word_Storage[j+1];
                                     word_Storage[j+1]=word_Storage[j];
                                     word_Storage[j]=temp;
                            }
                                  }
              }
       public void wordExtractor() {
              int i;
              char[] temp;
              //counting the number of words
              for(i=0;i<msg.length();i++)
            if(msg.charAt(i)== ' ')
                   word_counter++;
        word_counter +=1;
        word_Storage = new String[word_counter];
        int k = 0;
        //extracting words
        for(i=0;i<msg.length();i++) {
               word_Storage[k]="";
               while(msg.charAt(i) != ' ' && msg.charAt(i) != '.' ) {
                             word_Storage[k] +=msg.charAt(i);
                             i++;
                            }
               // converting first character to upper case
               temp=word_Storage[k].toCharArray();// converting to char array from String
               word_Storage[k]="";
               temp[0] = Character.toUpperCase(temp[0]); // first letter to upper case
               word_Storage[k] = String.copyValueOf(temp); // converting to String from char array
               k++;
               }
  }
      
       public void deleteExtraSpaces() {
              int counter,start;
              String temp="";
              for(int i=0;i<msg.length();i++) {
                     counter=0;
                     start=i;
                     while(msg.charAt(i)!= ' ' && i<msg.length() && msg.charAt(i)!= '.')
                           i++;
                     counter=i;
                     temp += msg.substring(start,counter+1);
                     counter +=1;
                     if(counter >= msg.length())
                           break;
                     while(msg.charAt(counter)==' ')
                           counter++;
                     i=counter-1;
                     }
              msg=temp;
       }
       SenetenceSort(String Sentence){
              msg=Sentence;
              word_counter=0;
       }
       private String msg;
       private int word_counter;
       private String[] word_Storage;
}


No comments:

Post a Comment