Thursday 19 January 2017

Reverse...

The input here will consists 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 a punctuation mark other than blanks. For example consider the following input text:
‘This is a sample piece of text to illustrate this problem. If you are smart you will solve this right’.
 The corresponding output would read as:
‘right this solve will you smart are you If problem this illustrate to text of piece sample a is This’.
That is, the lines are printed in reverse order. Note: Individual words are not reversed.
Input format
The first line of input contains a single integer N( < = 20), indicating the number of lines in the input. This is followed by N 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.
Input:                    Emotions, controlled and directed to work, is character. By Swami Vivekananda.
Output:                  Vivekananda Swami By character is work to directed and controlled Emotions.

Input :                   Do not judge a book by its cover.
Output:                  cover its by book a judge not Do.



Solution:-

import java.util.*;
class reverse
{
     String str;
     StringTokenizer stk;
     String arr[]=new String[60];
     int index=0;
     String s1;
     public void take()
     {
           Scanner sc=new Scanner(System.in);
           System.out.println("Enter the sentence:");
           str=sc.nextLine();
           str=str.substring(0,str.length()-1);
           stk=new  StringTokenizer(str);
    while(stk.hasMoreTokens())
           {
                s1=stk.nextToken();
                if((s1.charAt(s1.length()-1) >=65 && s1.charAt(s1.length()-1)<=90)||(s1.charAt(s1.length()-1) >=97 && s1.charAt(s1.length()-1)<=122))
           arr[index++]=s1;
           else
           arr[index++]=s1.substring(0,s1.length()-1);
    }
    str=" ";
    for(int i=index-1;i >=0;i--)
    {
           str=str+ arr[i]+" ";
    }
    str=str.trim();
    str=str+".";
     }
     public void display()
     {
           System.out.println("Output="+str);
     }
     public static void main(String args[])
     {
           reverse obj=new reverse();
           obj.take();
           obj.display();
     }

}






No comments:

Post a Comment

Goldbach Number ISC 2018

Question: A Goldbach number is a positive even integer that can be expressed as the sum of two odd primes. Note: All even integer numbers ...