Write a program to read several lines from a file called H12.in. For each line,
ID: 3864291 • Letter: W
Question
Write a program to read several lines from a file called H12.in. For each line, check whether it is a palindrome. For each line read from the file, print only the lines that are palindromes. Recall that a palindrome is a string that is the same backwards and forwards, ignoring spaces and punctuation. Some example palindromes are “radar” and “I, Madam, I made radio. So I dared! Am I mad? Am I??” You may use theremoveWS method from Lab 14. Your palindrome checking method must be recursive.
Write a program to read several lines from a file called H12.in. For each line, check whether it is a palindrome. For each line read from the file, print only the lines that are palindromes. Recall that a palindrome is a string that is the same backwards and forwards, ignoring spaces and punctuation. Some example palindromes are “radar” and “I, Madam, I made radio. So I dared! Am I mad? Am I??” You may use theremoveWS method from Lab 14. Your palindrome checking method must be recursive.
Write a program to read several lines from a file called H12.in. For each line, check whether it is a palindrome. For each line read from the file, print only the lines that are palindromes. Recall that a palindrome is a string that is the same backwards and forwards, ignoring spaces and punctuation. Some example palindromes are “radar” and “I, Madam, I made radio. So I dared! Am I mad? Am I??” You may use theremoveWS method from Lab 14. Your palindrome checking method must be recursive.
H12.in:
Explanation / Answer
package test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
try {
File file = new File("input.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
List<String> list = new ArrayList<String>();
String line;
while ((line = bufferedReader.readLine()) != null) {
line=line.replaceAll("[\s'\?"\;\.\,\:]", "").toLowerCase();
list.add(line);
}
System.out.println("Palindrome lines are as follows :");
for(String s:list){
StringBuffer sf = new StringBuffer(s);
if(sf.reverse().toString().equals(s)){
System.out.println(s);
}
}
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.