KnowledgeBoat Logo

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

Java

Java String Handling

7 Likes

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

BlueJ output of 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 DoBlueJ output of 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

Answered By

3 Likes


Related Questions