Computer Science
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 Emotions
Input:
1
Do not judge a book by its cover.
Output:
cover its by book a judge not Do
Answer
import java.util.*;
public class KboatTextReverse
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number of lines (n): ");
int n = in.nextInt();
in.nextLine();
if (n < 1 || n > 20) {
System.out.println("Invalid number of lines");
return;
}
String punctuations = "'.,;:";
System.out.println("Enter Lines:");
String lines[] = new String[n];
for (int i = 0; i < n; i++) {
lines[i] = in.nextLine();
if (lines[i].length() > 80) {
System.out.println("Invalid line!");
System.out.println("Length should be within 80 characters");
return;
}
}
StringBuffer sbLine = new StringBuffer();
for (int i = 0; i < n; i++) {
StringTokenizer st = new StringTokenizer(lines[i]);
while (st.hasMoreTokens()) {
StringBuffer sbWord = new StringBuffer();
String word = st.nextToken();
int len = word.length();
for (int j = 0; j < len; j++) {
char ch = word.charAt(j);
if (!Character.isWhitespace(ch)
&& punctuations.indexOf(ch) == -1) {
sbWord.append(ch);
}
}
sbLine.insert(0, sbWord.toString());
sbLine.insert(0, " ");
}
}
String reversedLine = sbLine.toString().trim();
System.out.println(reversedLine);
}
}
Output
Related Questions
Write 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 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:
ValidWhile typing, a typist has created two or more consecutive blank spaces between the words of a sentence. Write a program in Java to eliminate multiple blanks between the words by a single blank.
Sample Input:
Indian Cricket team tour to AustraliaSample Output:
Indian Cricket team tour to AustraliaRead 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.