‹ Back to the paper
Write the program described below.
Write a program to accept a sentence which may be terminated by either ‘.’ , ‘?’ or ‘!’ only. The…
Write a program to accept a sentence which may be terminated by either ‘.’ , ‘?’ or ‘!’ only. The words are to be separated by a single blank space and are in lower case. Perform the following tasks:
(a) Check for the validity of the accepted sentence and for the terminating character.
(b) Arrange the words contained in the sentence according to the size of the words in ascending order. If two words are of the same length then the first occurring comes first. The sentence should begin with a capital alphabet in both the cases i.e. Input and Output.
(c) Display both the sentences separately with each sentence beginning with a capital alphabet.
Design your program which will enable the output in the format given below:
Sample input/output
Sample 1
INPUT: the lines are printed in reverse order.
OUTPUT:
The lines are printed in reverse order.
In the are lines order printed reverse.
Sample 2
INPUT: print the sentence in ascending order.
OUTPUT:
Print the sentence in ascending order.
In the print order sentence ascending.
Sample 3
INPUT: i love my country.
OUTPUT:
I love my country.
I my love country.Answer
Answer
AI2(a): This part checks the validity and the terminating character. The complete program for parts (a), (b) and (c) is:
import java.util.Scanner;
public class SentenceSort
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("INPUT: ");
String s = sc.nextLine();
// (a) validity of the sentence and of the terminating character
boolean valid = s.length() > 1;
if (valid)
{
char last = s.charAt(s.length() - 1);
if (last != '.' && last != '?' && last != '!')
valid = false;
}
if (valid)
{
String body = s.substring(0, s.length() - 1);
if (body.startsWith(" ") || body.endsWith(" ") || body.indexOf(" ") != -1)
valid = false;
for (int i = 0; i < body.length() && valid; i++)
{
char c = body.charAt(i);
if (!(c >= 'a' && c <= 'z') && c != ' ')
valid = false;
}
}
if (!valid)
{
System.out.println("INVALID INPUT");
return;
}
char term = s.charAt(s.length() - 1);
String body = s.substring(0, s.length() - 1);
String w[] = body.split(" ");
int n = w.length;
// (b) arrange the words by length (insertion sort keeps the original order of equal lengths)
for (int i = 1; i < n; i++)
{
String key = w[i];
int j = i - 1;
while (j >= 0 && w[j].length() > key.length())
{
w[j + 1] = w[j];
j--;
}
w[j + 1] = key;
}
String sorted = "";
for (int i = 0; i < n; i++)
sorted = sorted + w[i] + " ";
sorted = sorted.trim();
// (c) display both sentences, each beginning with a capital letter
String first = Character.toUpperCase(body.charAt(0)) + body.substring(1) + term;
String second = Character.toUpperCase(sorted.charAt(0)) + sorted.substring(1) + term;
System.out.println();
System.out.println("OUTPUT:");
System.out.println(first);
System.out.println(second);
}
}Validity check: the sentence is accepted only if its last character is '.', '?' or '!', it has at least one word, it has no leading or trailing blank, no two blanks together (words are separated by a single blank) and every other character is a lower case letter. Otherwise the message INVALID INPUT is printed and the program stops. Tested: 'hello world' (no terminating character) and 'two spaces here.' (double blank) both give INVALID INPUT.
2(b): The sentence (without the terminating character) is split into words with split(" "). The words are arranged in ascending order of their length with insertion sort:for (int i = 1; i < n; i++)
{
String key = w[i];
int j = i - 1;
while (j >= 0 && w[j].length() > key.length())
{
w[j + 1] = w[j];
j--;
}
w[j + 1] = key;
}A word is shifted only when the word before it is strictly longer, so words of the same length keep their original order (the first occurring comes first). Example: 'print the sentence in ascending order' becomes in the print order sentence ascending (in = 2, the = 3, print = 5, order = 5, sentence = 8, ascending = 9 letters).
2(c): Both sentences are displayed with the first letter changed to a capital letter using Character.toUpperCase() and with the original terminating character added at the end:String first = Character.toUpperCase(body.charAt(0)) + body.substring(1) + term;
String second = Character.toUpperCase(sorted.charAt(0)) + sorted.substring(1) + term;
System.out.println(first);
System.out.println(second);Tested with the three samples:
INPUT: the lines are printed in reverse order.
OUTPUT:
The lines are printed in reverse order.
In the are lines order printed reverse.
INPUT: print the sentence in ascending order.
OUTPUT:
Print the sentence in ascending order.
In the print order sentence ascending.
INPUT: i love my country.
OUTPUT:
I love my country.
I my love country.From ISC 2021 Specimen Computer Science Paper 2, question 2.