Write a program to read several lines from a file called H.in. For each line, ch
ID: 3864337 • Letter: W
Question
Write a program to read several lines from a file called H.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. Your palindrome checking method must be recursive. In JAVA:
removeWS :
public static String removeWS(String a)
{
if (a.length() == 0)
return a;
String b = Character.isLetterOrDigit(a.substring(0, 1).charAt(0)) ? a.substring(0, 1) : "";
return b + removeWS(a.substring(1));
}
}
H.in:
You must be the change you want to see in the world
Sore was I, ere I saw Eros.
A man, a plan, a canal -- Panama
Never a foot too far, even.
BORROW - OR ROB?
Euston SaW I was not Sue.
THIS IS JUST A TEST.
"Stop!" nine myriad murmur. "Put up rum, rum, dairymen, in pots."
Live on evasions?! No, I save no evil.
Red Roses run no risk, sir, on nurses order.
The best way out is always through........
Explanation / Answer
// PalindromeRecursion.java
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.File;
public class PalindromeRecursion
{
public static String removeWS(String a)
{
if (a.length() == 0)
return a;
String b = Character.isLetterOrDigit(a.substring(0, 1).charAt(0)) ? a.substring(0, 1) : "";
return b + removeWS(a.substring(1));
}
// check palindrome
public static boolean isPalindrome(String s)
{
if(s.length() == 0 || s.length() == 1)
return true;
if(s.charAt(0) == s.charAt(s.length()-1))
return isPalindrome(s.substring(1, s.length()-1));
return false;
}
// method o read file
public static void readFile() throws IOException
{
File file = new File("H.in");
FileInputStream fileStream = new FileInputStream(file);
BufferedReader bufferRead = new BufferedReader(new InputStreamReader(fileStream));
String line = null;
String s = null;
while ((line = bufferRead.readLine()) != null)
{
s = removeWS(line);
s = s.toLowerCase();
if(isPalindrome(s) == true)
System.out.println(line);
}
bufferRead.close();
}
public static void main(String[] args)
{
try
{
readFile();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
/*
output:
Sore was I, ere I saw Eros.
A man, a plan, a canal -- Panama
Never a foot too far, even.
BORROW - OR ROB?
Euston SaW I was not Sue.
"Stop!" nine myriad murmur. "Put up rum, rum, dairymen, in pots."
Live on evasions?! No, I save no evil.
Red Roses run no risk, sir, on nurses order.
H.in
You must be the change you want to see in the world
Sore was I, ere I saw Eros.
A man, a plan, a canal -- Panama
Never a foot too far, even.
BORROW - OR ROB?
Euston SaW I was not Sue.
THIS IS JUST A TEST.
"Stop!" nine myriad murmur. "Put up rum, rum, dairymen, in pots."
Live on evasions?! No, I save no evil.
Red Roses run no risk, sir, on nurses order.
The best way out is always through........
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.