Categories
Class 11 Class 12

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;
    }
}

2 replies on “Generate Palindrome Words ISC 2019 Practical”

Your generate method does not work. Please check on it, anyways thanks for your contributions to students like me.

The generate method works fine. The program is already tested and it gives correct output for all the given cases.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.