Computer Science
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:
- Check for the validity of the accepted sentence only for the terminating character.
- Arrange the words in ascending order of their length. If two or more words have the same length, then sort them alphabetically.
- 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
Java
Java String Handling
ICSE Prac 2020
29 Likes
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
![BlueJ output of 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: (a) Check for the validity of the accepted sentence only for the terminating character. (b) Arrange the words in ascending order of their length. If two or more words have the same length, then sort them alphabetically. (c) 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 BlueJ output of 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: (a) Check for the validity of the accepted sentence only for the terminating character. (b) Arrange the words in ascending order of their length. If two or more words have the same length, then sort them alphabetically. (c) 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](https://cdn1.knowledgeboat.com/img/abp12/1/2020-40-p3-1.jpg)
![BlueJ output of 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: (a) Check for the validity of the accepted sentence only for the terminating character. (b) Arrange the words in ascending order of their length. If two or more words have the same length, then sort them alphabetically. (c) 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 BlueJ output of 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: (a) Check for the validity of the accepted sentence only for the terminating character. (b) Arrange the words in ascending order of their length. If two or more words have the same length, then sort them alphabetically. (c) 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](https://cdn1.knowledgeboat.com/img/abp12/1/2020-40-p3-2.jpg)
![BlueJ output of 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: (a) Check for the validity of the accepted sentence only for the terminating character. (b) Arrange the words in ascending order of their length. If two or more words have the same length, then sort them alphabetically. (c) 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 BlueJ output of 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: (a) Check for the validity of the accepted sentence only for the terminating character. (b) Arrange the words in ascending order of their length. If two or more words have the same length, then sort them alphabetically. (c) 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](https://cdn1.knowledgeboat.com/img/abp12/1/2020-40-p3-3.jpg)
![BlueJ output of 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: (a) Check for the validity of the accepted sentence only for the terminating character. (b) Arrange the words in ascending order of their length. If two or more words have the same length, then sort them alphabetically. (c) 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 BlueJ output of 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: (a) Check for the validity of the accepted sentence only for the terminating character. (b) Arrange the words in ascending order of their length. If two or more words have the same length, then sort them alphabetically. (c) 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](https://cdn1.knowledgeboat.com/img/abp12/1/2020-40-p3-4.jpg)
Answered By
8 Likes
Related Questions
Define a class to accept a string and convert the same to uppercase, create and display the new string by replacing each vowel by immediate next character and every consonant by the previous character. The other characters remain the same.
Example:
Input : #IMAGINATION@2024
Output : #JLBFJMBSJPM@2024The output of the statement "CONCENTRATION".indexOf('T') is:
- 9
- 7
- 6
- (-1)
The output of the statement "talent".compareTo("genius") is:
- 11
- –11
- 0
- 13
A shopping website offers a special discount if the order ID has the sequence 555 anywhere in it. For example, 158545553031, 198555267140, …. .
Fill in the blanks (a) and (b) in the given Java Method to convert the order ID (a long integer) into a string and check if the sequence 555 is present in it.
void checkOrder(long oid) { String str = _______(a)_________; if(______(b)_______) { System.out.println("Special Discount Eligible: " + oid); } }