Question 3
ISC COMPUTER SCIENCE PRACTICAL 2019 QUESTION 3 : STRING
Write a program to accept a sentence which may be terminated by either '.', '?' or '!'
only. The words are to be entered by s asingle blank space and re in UPPER CASE.
Perform the following tasks:
(a) Check for validity for the entered senctence.
(b) Convert the non-palidrome words of the sentence into palidrome words by
concatenating the word by its revers (excluding the last character).
Example: The reverse of the word HELP would be LEH (ommiting the
last alphabet) and by concatenating both, the new palidrome word is HELPLEH.
Thus the word HELP become HELPLEH
Note: The words which end with repeated alphabets, for exaple ABB would
becoem ABBA and not ABBBA and XAZZZ becomes XAZZZAX.
(c) Display the original sentence along with converted sentence.
The logic behind the program is as follows:
1. Take input and check for validity
2. Extract individual words from the sentence and store
3. Now for each word check whether it is palidrome or not
4. If it is palidrome print as it is.
5. If the word is not a palidrome, we need to convert it and print.
I have used the method public void wordExtractor() to store the words.
Extracting words from a sentence is an important part of ISC Computer
Science Practical. The code for it is self expalined!
Also to test whethere the word is palidrome or not, I have used the
method "private boolean IsPalindrome(String s)". It returns 'true' if the
word is a palindrome else it returns false. The working is also simple
and the code will reveal the idea.
Now, the most important method of this program is
"private String ConvertToPalindrome(String s)" which convert a word to a
palindromic word under the given conditions. To understand this part, let us
take a example of a non-palindromic word "XAZZZ".
Clearly the word is non-palindrome.
To make it palidrome you can reflect it by putting a mirror after the
last character: XAZZZ|ZZZAX which gives XAZZZZZXAX and this is clearly
a palindrome.
But see that the word can also be converted to a palidrome word by simply
adding "AX" at the end: "XAZZZ"+"AX" = "XAZZZAX" and this requires less
characters compared to the first case, and this is what has been asked in the
question to do.
For this, the logic is that starting form the last character of the word,
we move the counter towards the first character. We stop at the point
where we get a different character compared to the preceeding character.
In our case we stop our counter at the position of 'A'. Then we go on
adding the characters at the last of the string from the postion of 'A' to
the first character of the word! And this completes teh process.
For "ASERDDDD", we stop at 'R' and add "RESA" to "ASERDDDD" to get
"ASERDDDD"+"RESA"="ASERDDDDRESA"
For "HELP", starting from the end character, we stop at the position of 'L'
since 'L' is different from 'P' the we add "LEH" to "HELP" to get
"HELP"+"LEH"="HELPLEH"!
This is coded as below:
COMPLETE JAVA CODE:
ISC COMPUTER SCIENCE PRACTICAL 2019 QUESTION 3 : STRING
Write a program to accept a sentence which may be terminated by either '.', '?' or '!'
only. The words are to be entered by s asingle blank space and re in UPPER CASE.
Perform the following tasks:
(a) Check for validity for the entered senctence.
(b) Convert the non-palidrome words of the sentence into palidrome words by
concatenating the word by its revers (excluding the last character).
Example: The reverse of the word HELP would be LEH (ommiting the
last alphabet) and by concatenating both, the new palidrome word is HELPLEH.
Thus the word HELP become HELPLEH
Note: The words which end with repeated alphabets, for exaple ABB would
becoem ABBA and not ABBBA and XAZZZ becomes XAZZZAX.
(c) Display the original sentence along with converted sentence.
The logic behind the program is as follows:
1. Take input and check for validity
2. Extract individual words from the sentence and store
3. Now for each word check whether it is palidrome or not
4. If it is palidrome print as it is.
5. If the word is not a palidrome, we need to convert it and print.
I have used the method public void wordExtractor() to store the words.
Extracting words from a sentence is an important part of ISC Computer
Science Practical. The code for it is self expalined!
Also to test whethere the word is palidrome or not, I have used the
method "private boolean IsPalindrome(String s)". It returns 'true' if the
word is a palindrome else it returns false. The working is also simple
and the code will reveal the idea.
Now, the most important method of this program is
"private String ConvertToPalindrome(String s)" which convert a word to a
palindromic word under the given conditions. To understand this part, let us
take a example of a non-palindromic word "XAZZZ".
Clearly the word is non-palindrome.
To make it palidrome you can reflect it by putting a mirror after the
last character: XAZZZ|ZZZAX which gives XAZZZZZXAX and this is clearly
a palindrome.
But see that the word can also be converted to a palidrome word by simply
adding "AX" at the end: "XAZZZ"+"AX" = "XAZZZAX" and this requires less
characters compared to the first case, and this is what has been asked in the
question to do.
For this, the logic is that starting form the last character of the word,
we move the counter towards the first character. We stop at the point
where we get a different character compared to the preceeding character.
In our case we stop our counter at the position of 'A'. Then we go on
adding the characters at the last of the string from the postion of 'A' to
the first character of the word! And this completes teh process.
For "ASERDDDD", we stop at 'R' and add "RESA" to "ASERDDDD" to get
"ASERDDDD"+"RESA"="ASERDDDDRESA"
For "HELP", starting from the end character, we stop at the position of 'L'
since 'L' is different from 'P' the we add "LEH" to "HELP" to get
"HELP"+"LEH"="HELPLEH"!
This is coded as below:
private String ConvertToPalindrome(String s) {
int counter;
for(counter=s.length()-1;counter>0;counter--) {
if(s.charAt(counter)!=s.charAt(counter-1))
break;
}
counter -=1;
for(int i=counter;i>=0;i--)
s += s.charAt(i);
return(s);
}
COMPLETE JAVA CODE:
import java.io.*;
public class ISC2019Q3 {
public static void main(String[] args) throws IOException{
String Sentence;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the sentence:");
Sentence=br.readLine();
Sentence = Sentence.toUpperCase();
while(Sentence.charAt(Sentence.length()-1)!= '?' && Sentence.charAt(Sentence.length()-1)!= '.' && Sentence.charAt(Sentence.length()-1)!= '!' ) {
System.out.println("INVALID INPUT:,TRY AGAIN");
Sentence=br.readLine();
}
Palindrome obj = new Palindrome(Sentence);
obj.wordCount();
obj.wordExtractor();
System.out.println("INPUT:"+Sentence);
System.out.println("OUTPUT:");
obj.display();
br.close();
}
}
class Palindrome{
public void display() {
for(int i=0;i<word_counter;i++) {
if(IsPalindrome(word_Storage[i]))
System.out.print(word_Storage[i]+"
");
else
System.out.print(ConvertToPalindrome(word_Storage[i])+"
");
}
}
private String
ConvertToPalindrome(String s) {
int counter;
for(counter=s.length()-1;counter>0;counter--) {
if(s.charAt(counter)!=s.charAt(counter-1))
break;
}
counter -=1;
for(int i=counter;i>=0;i--)
s += s.charAt(i);
return(s);
}
private boolean IsPalindrome(String s) {
flag = true;
for(int i=0;i<s.length();i++)
if(s.charAt(i)!=s.charAt(s.length()-1-i)) {
flag = false;
break;
}
return(flag);
}
public void wordExtractor() {
int k = 0;
for(int i=0;i<msg.length();i++) {
word_Storage[k]="";
while(msg.charAt(i) != ' ' && msg.charAt(i) != '?' && msg.charAt(i) != '.' && msg.charAt(i) != '!') {
word_Storage[k] +=msg.charAt(i);
i++;
}
k++;
}
}
public void wordCount() {
for(int i=0;i<msg.length();i++)
if(msg.charAt(i)== ' ')
word_counter++;
word_counter +=1;
word_Storage = new String[word_counter];
}
Palindrome (String Sentence) {
msg = Sentence;
word_counter = 0;
}
private boolean flag;
private int word_counter;
private String msg;
private String[] word_Storage;
}
No comments:
Post a Comment