ISC practical papers › ISC 2005 › Question 1
Title Case
ISC 2005 Computer Science Practical, Question 1 · Strings
The question
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 lengths. Same length words should be sorted alphabetically.
Each word must star 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.
Question: ISC 2005 Computer Science Paper 2 (Practical), Question 1, set by the Council for the Indian School Certificate Examinations (CISCE). Solution and video: Vinay Singh.
Java solution
import java.util.*;
class MyString {
private String sentence, words[], output;
public MyString(String sentence) {
this.sentence = sentence.toLowerCase();
arrange();
}
private void arrange() {
String temp;
boolean swapped;
words = sentence.split("[ .]+");
for (int i = 0; i < words.length - 1; i++) {
swapped = false;
for (int x = 0; x < words.length - 1 - i; x++) {
if (words[x + 1].length() > words[x].length()) {
temp = words[x + 1];
words[x + 1] = words[x];
words[x] = temp;
swapped = true;
} else if (words[x + 1].length() == words[x].length()) {
if (words[x + 1].compareTo(words[x]) < 0) {
temp = words[x + 1];
words[x + 1] = words[x];
words[x] = temp;
swapped = true;
}
}
}
if (!swapped)
break;
}
assemble();
}
private void assemble() {
output = "";
for (String word : words) {
output += Character.toUpperCase(word.charAt(0)) + word.substring(1) + " ";
}
output = output.trim() + ".";
}
public String toString() {
return output;
}
}
class ISC2005Q1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String sentence;
System.out.println("INPUT:");
sentence = in.nextLine();
MyString str = new MyString(sentence);
System.out.println("OUTPUT:\n" + str);
in.close();
}
}
All these solutions in one free book
Pragmatic Solutions to ISC Computer Science Practical Problems collects every program from 2004 to 2021 in a 202-page PDF.