Saturday , 22 July 2017
Home » ISC Important Programs » Program to input multiple sentences and reverse it without Punctuation marks

Program to input multiple sentences and reverse it without Punctuation marks

Question:

The input in this question will consist of a number of lines of English text consisting of the letters of the English alphabets, the punctuation marks (‘) apostrophe, (.) full stop, (,) comma , (;) semicolon , (:) colon and white space. Write a program to print the words of the input in reverse order without any punctuation marks other than blanks.

For example, Consider the following input text:

INPUT:
Enter number of sentences: 2
Enter the sentences:
This is a sample piece of text to illustrate this question
if you are smart you will solve this right.

OUTPUT: right this solve will you smart are you if question this illustrate to text of piece sample a is this

NOTE : Individual words (i.e. characters of every word) are not reversed

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

Sample Input 1 :
Enter number of sentences: 1
Enter the text:
Do not judge a book by its cover.

Sample Output: Cover its by book a judge not do

Sample Input 2:
Enter number of sentences: 2
Enter the text:
Emotions, controlled and directed to work, is character.
By Swami Vivekananda.

Sample Output: Vivekananda Swami By character is work to dedicated and controlled Emotions

Programming Code:

/**
* The class Sent_Merge_Rev inputs multiple sentences and prints the words in reverse order
* without any punctuation marks other than blanks
* @author : www.javaforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.io.*;
import java.util.*;
class Sent_Merge_Rev
{
    public static void main(String args[])throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter the number of sentences: "); //inputting the number of sentences to accept
        int n = Integer.parseInt(br.readLine());
        String s = "";

        for(int i=1; i<=n; i++)
        {
            System.out.print("Enter Sentence "+i+": ");
            s = s + br.readLine(); //inputting multiple sentences and joining them in the same String
        }
        
        //converting the sentence into StringTokenizer giving the given punctuation marks

        StringTokenizer str=new StringTokenizer(s," '.,;:!?");
        int c=str.countTokens();

        String w="", rev="";
        for(int i=1; i<=c; i++)
        {
            w = str.nextToken(); //extracting one word at a time
            rev = w+" "+rev; //joining the extracted words in reverse order
        }
        
        System.out.println("Output: "+rev);
    }
}

Output:

Example 1:
Enter the number of sentences: 2
Enter Sentence 1: Emotions, controlled and directed to work, is character.
Enter Sentence 2: By Swami Vivekananda.
Output: Vivekananda Swami By character is work to directed and controlled Emotions

Example 2:
Enter the number of sentences: 3
Enter Sentence 1: Did you know?
Enter Sentence 2: ICSE and ISC computer is really easy.
Enter Sentence 3: Thanks to javaforschool!
Output: javaforschool to Thanks easy really is computer ISC and ICSE know you Did

Check Also

[Question 2] ISC 2017 Computer Practical Paper Solved – Quiz Result

Solution of Program 2 of ISC 2017 Computer Science Paper 2 (Practical) Exam. Java program to input the answers of each participant row-wise and calculate their marks

2 comments

  1. can we use multiple delimeters in String Tokenizers?

Leave a Reply

Your email address will not be published. Required fields are marked *