ISC practical papers › ISC 2019 › Question 3
Convert words to Palindromes
ISC 2019 Computer Science Practical, Question 3 · Strings
The question
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 UPPER CASE.
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
Question: ISC 2019 Computer Science Paper 2 (Practical), Question 3, set by the Council for the Indian School Certificate Examinations (CISCE). Solution and video: Vinay Singh.
Java solution
import java.util.*;
public class ISC2019Q3 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("INPUT");
String sentence=in.nextLine();
MyString str = new MyString(sentence);
System.out.println("OUPUT");
if(str.isValid()){
System.out.println(sentence);
str.convertToPalindrome();
System.out.println(str);
}else{
System.out.println("INVALID INPUT");
}
in.close();
}
}
class MyString{
private String sentence, words[];
public MyString(String sentence){
this.sentence=sentence;
}
public boolean isValid(){
return ".?!".indexOf(sentence.charAt(sentence.length()-1))>=0;
}
public void convertToPalindrome(){
String reverse="";
int delta;
sentence=sentence.substring(0, sentence.length()-1);
words=sentence.split(" ");
for( int i=0; i<words.length;i++){
if(!isPalindrome(words[i])){
reverse="";
if(words[i].length()>2 && words[i].charAt(words[i].length()-1)==words[i].charAt(words[i].length()-2)){
delta=1;
}else{
delta=0;
}
for(int position=0; position<words[i].length()-1-delta;position++){
reverse=words[i].charAt(position)+reverse;
}
words[i]=words[i]+reverse;
}
}
}
public static boolean isPalindrome(String source){
int half=source.length()/2;
for( int position=0; position<=half;position++){
if(source.charAt(position)!=source.charAt(source.length()-1-position))
return false;
}
return true;
}
public String toString(){
String output="";
for(String word:words){
output+=word+" ";
}
return output;
}
}
All these solutions in one free book
Pragmatic Solutions to ISC Computer Science Practical Problems collects every program from 2004 to 2021 in a 202-page PDF.