KnowledgeBoat Logo

Computer Science

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 3

INPUT:
YOU MUST BE CRAZY#

OUTPUT:
INVALID INPUT

Java

Java String Handling

ICSE Prac 2019

18 Likes

Answer

import java.util.*;

public class Palindrome
{

    public static boolean isPalindrome(String word) {
        boolean palin = true;
        
        int len = word.length();
        for (int i = 0; i <= len / 2; i++) {
            if (word.charAt(i) != word.charAt(len - 1 - i)) {
                palin = false;
                break;
            }
        }
        
        return palin;
    }
    
    public static String makePalindrome(String word) {
        int len = word.length();
        char lastChar = word.charAt(len - 1);
        int i = len - 1;
        while (word.charAt(i) == lastChar) {
            i--;
        }
        
        StringBuffer sb = new StringBuffer(word);
        for (int j = i; j >= 0; j--) {
            sb.append(word.charAt(j));
        }
        
        return sb.toString();
    }
    
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("ENTER THE SENTENCE:");
        String ipStr = in.nextLine().trim().toUpperCase();
        int len = ipStr.length();
        
        char lastChar = ipStr.charAt(len - 1);
        if (lastChar != '.' 
            && lastChar != '?' 
            && lastChar != '!') {
                System.out.println("INVALID INPUT");
                return;
        }
        
        String str = ipStr.substring(0, len - 1);
        
        StringTokenizer st = new StringTokenizer(str);
        StringBuffer sb = new StringBuffer();
        
        while (st.hasMoreTokens()) {
            String word = st.nextToken();
            boolean isPalinWord = isPalindrome(word);
            if (isPalinWord) {
                sb.append(word);
            }
            else {
                String palinWord = makePalindrome(word);
                sb.append(palinWord);
            }
            sb.append(" ");
        }

        String convertedStr = sb.toString().trim();
        
        System.out.println();
        System.out.println(ipStr);
        System.out.println(convertedStr);
    }
}

Output

BlueJ output of 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 3 INPUT: YOU MUST BE CRAZY# OUTPUT: INVALID INPUTBlueJ output of 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 3 INPUT: YOU MUST BE CRAZY# OUTPUT: INVALID INPUTBlueJ output of 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 3 INPUT: YOU MUST BE CRAZY# OUTPUT: INVALID INPUTBlueJ output of 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 3 INPUT: YOU MUST BE CRAZY# OUTPUT: INVALID INPUT

Answered By

6 Likes


Related Questions