[go: up one dir, main page]

0% found this document useful (0 votes)
61 views6 pages

Program 7

The document describes a program that takes English text as input, consisting of letters, punctuation, and whitespace. It outputs the words of the text in reverse order without punctuation, except for spaces. It provides an example input and output. The input format is specified as the number of lines followed by the lines of text, with each line accepting up to 80 characters. The solution uses a StringTokenizer to extract each word, reverses the order of characters in each word while removing punctuation, and outputs the result.

Uploaded by

Khushi Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views6 pages

Program 7

The document describes a program that takes English text as input, consisting of letters, punctuation, and whitespace. It outputs the words of the text in reverse order without punctuation, except for spaces. It provides an example input and output. The input format is specified as the number of lines followed by the lines of text, with each line accepting up to 80 characters. The solution uses a StringTokenizer to extract each word, reverses the order of characters in each word while removing punctuation, and outputs the result.

Uploaded by

Khushi Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Program – 7

Program Statement:
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.

Input:1
Do not judge a book by it’s cover.

Output: cover its by book a judge not Do


SOLUTION:
import java.util.*;

class TextReverse

public static void main()

Scanner sc = new Scanner(System.in);

System.out.print("Enter number of lines (n): ");

int n = sc.nextInt();

sc.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] = sc.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:
Input - Enter number of lines (n): 1
Enter Lines:

Do not judge a book by it's cover.

Output - cover its by book a judge not Do


Algorithm:
Step-1: Start
Step-2: Enter number of lines (n)
Step-3: If number of lines are less than 1 or greater than
20 print "Invalid number of lines".
Step-4: Enter the lines.
Step-5: Initialise of an array n.
Step-6: Through a loop input the lines in the array
Step-7: If any line consists of more than 10 characters
print "Invalid line!" and "Length should be within 80
characters".
Step-8: Extract each word of a line.
Step-9: Store the length of each word.
Step-10: Extract each character.
Step-11: Add each character except for punctuation
marks to print the line in reverse order.
Step-12: Stop.

You might also like