Computer Science
Read a single sentence which terminates with a full stop (.). The words are to be separated with a single blank space and are in lower case. Arrange the words contained in the sentence according to the length of the words in ascending order. If two words are of the same length then the word occurring first in the input sentence should come first. For both, input and output the sentence must begin in upper case.
Test your program for given data and also some random data:
Input:
The lines are printed in reverse order.
Output:
In the are lines order printed reverse.
Input:
Print the sentence in ascending order.
Output:
In the print order sentence ascending.
Input:
I love my Country.
Output:
I my love Country.
Java
Java String Handling
20 Likes
Answer
import java.util.*;
public class KboatWordLenSort
{
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();
if (str.charAt(len - 1) != '.') {
System.out.println("Invalid Input!");
System.out.println("Sentence should end with full stop.");
return;
}
if (Character.isLowerCase(str.charAt(0))) {
System.out.println("Invalid Input!");
System.out.println("Sentence should start with upper case letter.");
return;
}
String ipStr = Character.toLowerCase(str.charAt(0)) + str.substring(1, len - 1);
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;
}
}
}
strArr[0] = Character.toUpperCase(strArr[0].charAt(0))
+ strArr[0].substring(1);
System.out.println("Sorted String:");
for (int i = 0; i < wordCount; i++) {
System.out.print(strArr[i]);
if (i == wordCount - 1) {
System.out.print(".");
}
else {
System.out.print(" ");
}
}
}
}
Output
Answered By
6 Likes
Related Questions
The input in this question will consist of a number of lines of English text consisting of the letters of the English alphabet, the punctuation marks (') apostrophe, (.) full stop (, ) comma, (; ) semicolon, (:) colon and white space characters (blank, new line). Your task is to print the words of the text in reverse order without any punctuation marks other than blanks.
For example, consider the following input text:
This is a sample piece of text to illustrate this question.
If you are smart you will solve this right.The corresponding output would read as:
right this solve will you smart are you if question this illustrate to text of piece sample a is this
Note: Individual words are not reversed.
Input Format:
This first line of input contains a single integer n ( < = 20), indicating the number of lines in the input.
This is followed by lines of input text. Each line should accept a maximum of 80 characters.Output Format:
Output the text containing the input lines in reverse order without punctuations except blanks as illustrated above.
Test your program for the following data and some random data.
Sample Data:
Input:
2
Emotions controlled and directed to work, is character.
By Swami Vivekananda.Output:
Vivekananda Swami By character is work to directed and controlled EmotionsInput:
1
Do not judge a book by its cover.Output:
cover its by book a judge not DoWrite a program to read a string / sentence and output the number of times each word occurs in the entire text. At the end, the output should be sorted into ascending order words along with usage of words. You may assume that the entire text is in capitals (you may also convert in capitals for your betterment) and each word is followed by a blank space except the last one, which is followed by a full stop. Let there be at the most 50 words in the text.
Test your program for the given sample data and also some other random data:
SAMPLE DATA:
Input:
YOU ARE GOOD WHEN YOUR THOUGHTS ARE GOOD AND YOUR DEEDS ARE GOOD.Output:
Words Word Count And 1 Are 3 Deeds 1 Good 3 Thoughts 1 When 1 You 1 Your 2 Input:
IF YOU FAIL TO PLAN YOU ARE PLANNING TO FAIL.Output:
Words Word Count Are 1 Fail 2 If 1 Plan 1 Planning 1 To 2 You 2 A string is said to be valid if it contains pair of parenthesis having text / alphabet such as (TY) and the string is said to be invalid if it contains nested parenthesis such as (Y (UP)).
For example:
SUN (A(X) RISE) BEGINS FROM (RT) EAST is an "Invalid" string because in this string nested parenthesis are present, but SUN (A) RISE BEGINS FROM (RT) EAST is a "Valid" string because there is no nested parenthesis.Write a program to:
- Read a string / sentence and convert in capitals.
- Check the validity of the given string.
- If the string is valid, output the given string omitting the portion enclosed in brackets otherwise, output a message "Sorry, and invalid string".
Test your program for the given sample data and also some other random data:
Sample Data:
Input:
SUN (a) RISE BEGINS FROM (RT) EAST
Output:
SUN RISE BEGINS FROM EASTInput:
SUN (A (X) RISE) BEGINS FROM (RT) EAST
Output:
Sorry, an invalid stringInput:
COM(IPX)PUTER IS (MY) JUNK (GOOD) SYSTEM
Output:
COMPUTER IS JUNK SYSTEMA new advanced Operating System, incorporating the latest hi-tech features has been designed by Opera Computer Systems. The task of generating copy protection code to prevent software privacy has been entrusted to the Security Department. The Security Department has decided to have codes containing a jumbled combination of alternate uppercase letters of the alphabet starting from A up to K (namely among A, C, E, G, I, K). The code may or may not be in the consecutive series of alphabets. Each code should not exceed 6 characters and there should be no repetition of characters. If it exceeds 6 characters, display an appropriate error message.
Write a program to input a code and its length. At the first instance of an error display "Invalid" stating the appropriate reason. In case of no error, display the message "Valid".
Sample Data:
Input:
n=4
ABCEOutput:
Invalid! Only alternate letters permitted!Input:
n=4
AcIKOutput:
Invalid! Only uppercase letters permitted!Input:
n = 7Output:
Error! Length of String should not exceed 6 characters!Input:
n=3
ACEOutput:
ValidInput:
n=5
GEAIKOutput:
Valid