Convert to Palindrome ISC Computer Science 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 words: 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:
THISIHT MOBILELIBOM APPA LOOKSKOOL FINENIF

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

import java.util.Scanner;
import java.util.StringTokenizer;
class Convert{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the sentence: ");
        String s = in.nextLine().toUpperCase();
        s = s.trim();
        if(!isValid(s)){
            System.out.println("INVALID INPUT");
            return;
        }
        StringTokenizer st = new StringTokenizer(s, " ?!.,;");
        String p = "";
        int count = st.countTokens();
        for(int i = 1; i <= count; i++){
            String word = st.nextToken();
            if(isPalindrome(word))
                p += word + " ";
            else
                p += toPalindrome(word) + " ";
        }
        System.out.println(s);
        System.out.println(p);
    }
    public static boolean isValid(String s){
        char last = s.charAt(s.length() - 1);
        if(last != '.' && last != '?' && last != '!')
            return false;
        if(s.indexOf("  ") >= 0)
            return false;
        return true;
    }
    public static boolean isPalindrome(String s){
        if(s.length() == 1)
            return true;
        int i = 0;
        int j = s.length() - 1;
        while(i < j){
            if(s.charAt(i) != s.charAt(j))
                return false;
            i++;
            j--;
        }
        return true;
    }
    public static String toPalindrome(String s){
        String p = new String(s);
        int i = s.length() - 2;
        while(p.charAt(p.length() - 1) == s.charAt(i))
            i--;
        while(i >= 0){
            p += s.charAt(i);
            i--;
        }
        return p;
    }
}

Comments

Popular posts from this blog

Encrypt Program ISC Specimen 2023 Theory

No Repeat Program ISC Computer Science 2022 Semester 2 Theory

Bank Inheritance Program ISC Specimen 2023 Theory