ICSE Class 12 Computer Science Question 3 of 3

Solved 2020 Practical Paper ISC Computer Science — Question 3

Back to all questions
3
Question

Question 3

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 UPPER CASE.

Perform the following tasks:

  1. Check for the validity of the accepted sentence only for the terminating character.
  2. Arrange the words in ascending order of their length. If two or more words have the same length, then sort them alphabetically.
  3. Display the original sentence along with the converted sentence.

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

Example 1:

INPUT:
AS YOU SOW SO SHALL YOU REAP.

OUTPUT:
AS YOU SOW SO SHALL YOU REAP.
AS SO SOW YOU YOU REAP SHALL

Example 2:

INPUT:
SELF HELP IS THE BEST HELP.

OUTPUT:
SELF HELP IS THE BEST HELP.
IS THE BEST HELP HELP SELF

Example 3:

INPUT:
BE KIND TO OTHERS.

OUTPUT:
BE KIND TO OTHERS.
BE TO KIND OTHERS

Example 4:

INPUT:
NOTHING IS IMPOSSIBLE#

OUTPUT:
INVALID INPUT

Solution
import java.util.*;

public class StringCheck
{
    public static String sortString(String ipStr) {
        StringTokenizer st = new StringTokenizer(ipStr);
        int wordCount = st.countTokens();
        String strArr[] = new String[wordCount];
        
        for (int i = 0; i < wordCount; i++) {
            strArr[i] = st.nextToken();
        }
        
        for (int i = 0; i < wordCount - 1; i++) {
            for (int j = 0; j < wordCount - i - 1; j++) {
                if (strArr[j].length() > strArr[j + 1].length()) {
                    String t = strArr[j];
                    strArr[j] = strArr[j+1];
                    strArr[j+1] = t;
                }
                
                if (strArr[j].length() == strArr[j + 1].length()
                    &&(strArr[j].compareTo(strArr[j+1]) > 0))
                {
                    String t = strArr[j];
                    strArr[j] = strArr[j+1];
                    strArr[j+1] = t;
                }
            }
        }
        
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < wordCount; i++) {
            sb.append(strArr[i]);
            sb.append(" ");
        }
        
        /*
         * trim will remove the extra space added
         * to the end of the string in the loop above
         */
        return sb.toString().trim();
    }
    
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a sentence:");
        String str = in.nextLine();
        int len = str.length();
        System.out.println();
        
        if (str.charAt(len - 1) != '.' 
            && str.charAt(len - 1) != '?' 
            && str.charAt(len - 1) != '!') {
                System.out.println("INVALID INPUT");
                return;
        }
        
        /*
         * str.substring(0, len - 1) removes the   
         * '.', '?', '!' at the end of the string
         */
        String sortedStr = sortString(str.substring(0, len - 1));
        
        System.out.println(str);
        System.out.println(sortedStr);
    }
}
Output
BlueJ output of StringCheck.java
BlueJ output of StringCheck.java
BlueJ output of StringCheck.java
BlueJ output of StringCheck.java
Answer

import
java.util.*
;
public
class
StringCheck
{
public
static
String
sortString
(
String
ipStr
) {
StringTokenizer
st
=
new
StringTokenizer
(ipStr);
int
wordCount
=
st
.
countTokens();
String
strArr[]
=
new
String
[wordCount];
for
(
int
i
=
0
; i
<
wordCount; i
++
) {
strArr[i]
=
st
.
nextToken();
}
for
(
int
i
=
0
; i
<
wordCount
-
1
; i
++
) {
for
(
int
j
=
0
; j
<
wordCount
-
i
-
1
; j
++
) {
if
(strArr[j]
.
length()
>
strArr[j
+
1
]
.
length()) {
String
t
=
strArr[j];
strArr[j]
=
strArr[j
+
1
];
strArr[j
+
1
]
=
t;
}
if
(strArr[j]
.
length()
==
strArr[j
+
1
]
.
length()
&&
(strArr[j]
.
compareTo(strArr[j
+
1
])
>
0
))
{
String
t
=
strArr[j];
strArr[j]
=
strArr[j
+
1
];
strArr[j
+
1
]
=
t;
}
}
}
StringBuffer
sb
=
new
StringBuffer
();
for
(
int
i
=
0
; i
<
wordCount; i
++
) {
sb
.
append(strArr[i]);
sb
.
append(
"
"
);
}
/*
* trim will remove the extra space added
* to the end of the string in the loop above
*/
return
sb
.
toString()
.
trim();
}
public
static
void
main
(
String
args
[]) {
Scanner
in
=
new
Scanner
(
System
.
in);
System
.
out
.
println(
"
Enter a sentence:
"
);
String
str
=
in
.
nextLine();
int
len
=
str
.
length();
System
.
out
.
println();
if
(str
.
charAt(len
-
1
)
!=
'
.
'
&&
str
.
charAt(len
-
1
)
!=
'
?
'
&&
str
.
charAt(len
-
1
)
!=
'
!
'
) {
System
.
out
.
println(
"
INVALID INPUT
"
);
return
;
}
/*
* str.substring(0, len - 1) removes the
* '.', '?', '!' at the end of the string
*/
String
sortedStr
=
sortString(str
.
substring(
0
, len
-
1
));
System
.
out
.
println(str);
System
.
out
.
println(sortedStr);
}
}
Output