C Programs Tutorials | IT Developer
IT Developer

Java Programs - Solved 2019 Practical Paper ISC Computer Science



Share with a Friend

Solved 2019 Practical Paper ISC Computer Science

Class 12 - ISC Computer Science Solved Practical Papers

Generate Palindrome Words - ISC 2019 Practical

Write a program to accept a sentence which may be terminated by either ‘.’, ‘?’ or ‘!’ only. The words are to be separated by a single blank space and are in uppercase.

Perform the following tasks:

a) Check for the validity of the accepted sentence.
b) Convert the non-palindrome words of the sentence into palindrome words by concatenating the word by its reverse (excluding the last character).

Example: The reverse of the word HELP would be LEH (omitting the last alphabet) and by concatenating both, the new palindrome word is HELPLEH. Thus, the word HELP becomes HELPLEH.
Note: The words which end with repeated alphabets, for example ABB would become ABBA and not ABBBA and XAZZZ becomes XAZZZAX.

[Palindrome word: Spells same from either side. Example: DAD, MADAM etc.]

c) Display the original sentence along with the converted sentence.

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

Example 1:
INPUT: THE BIRD IS FLYING.
OUTPUT:
THE BIRD IS FLYING.
THEHT BIRDRIB ISI FLYINGNIYLF

Example 2:
INPUT: IS THE WATER LEVEL RISING?
OUTPUT:
IS THE WATER LEVEL RISING?
ISI THEHT WATERETAW LEVEL RISINGNISIR

Example 3:
INPUT: THIS MOBILE APP LOOKS FINE.
OUTPUT:
THIS MOBILE APP LOOKS FINE.
THISIHT MOBILELIBOM APPA LOOKSKOOL FINENIF

Example 4:
INPUT: YOU MUST BE CRAZY#
OUTPUT:
INVALID INPUT

import java.io.*; import java.util.StringTokenizer; class Palindrome{ public static void main(String args[])throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Sentence: "); String s = br.readLine().trim().toUpperCase(); char ch = s.charAt(s.length() - 1); if(ch != \'.\' && ch != \'?\' && ch != \'!\'){ System.out.println("INVALID INPUT"); return; } StringTokenizer st = new StringTokenizer(s, " ?.!,"); int count = st.countTokens(); String p = new String(); for(int i = 1; i <= count; i++){ String word = st.nextToken(); if(isPalindrome(word)) p += word + " "; else p += generate(word) + " "; } System.out.println(s); System.out.println(p); } public static boolean isPalindrome(String w){ String r = new String(); for(int i = w.length() - 1; i >= 0; i--) r += w.charAt(i); return (w.equalsIgnoreCase(r)); } public static String generate(String w){ String r = new String(); for(int i = w.length() - 2; i >= 0; i--) r += w.charAt(i); while(w.length() > 1 && w.charAt(w.length() - 1) == w.charAt(w.length() - 2)) w = w.substring(0, w.length() - 1); return w + r; } }

Output

OUTPUT 1:
Sentence: THE BIRD IS FLYING.
THE BIRD IS FLYING.
THEHT BIRDRIB ISI FLYINGNIYLF  


OUTPUT 2:

Sentence: IS THE WATER LEVEL RISING?
IS THE WATER LEVEL RISING?
ISI THEHT WATERETAW LEVEL RISINGNISIR