DO NOT USE STRINGBUFFER OR STRING BUILDER. DO NOT CREATE A REVERSAL OF THE STRIN
ID: 3749246 • Letter: D
Question
DO NOT USE STRINGBUFFER OR STRING BUILDER. DO NOT CREATE A REVERSAL OF THE STRING AND THEN TEST FOR EQUALITY. YOU MUST CLEAN THE STRING AND WRITE A LOOP THAT COMPARES MIRRORED CHARACTERS.
The first method: static String toAlphaLowerCase( String s ) takes a String parameter and returns a different String that consists only of letters of the alphabet converted to lower case. This form is easier to test for palindrome.
The second method: static boolean isPalandrome( String s ) takes a String parameter and returns true if and only if the String is a palindrome. An empty String "" is by definition a palindrome since there is also no antisymmetry in it. A String consisting of a single character (.length()==1) is also a palindrome. Here is the code:
// Lab3.java Starter File
import java.io.*; // BufferedReader
import java.util.*; // Scanner
public class Lab3
{
public static void main (String args[]) throws Exception // i.e. the input file you put on cmd line is not in directory
{
// ALWAYS TEST FIRST TO VERIFY USER PUT REQUIRED INPUT FILE NAME ON THE COMMAND LINE
if (args.length < 1 )
{
System.out.println(" usage: C:\> java Lab2 <input filename> "); // i.e. C:> java Lab2 input.txt
System.exit(0);
}
BufferedReader infile = new BufferedReader (new FileReader( args[0] )); // we read our text file line by line
int lineNum=0;
while( infile.ready() )
{
String line = toAlphaLowerCase(infile.readLine());
if ( isPalindrome( line ) )
System.out.format("<%s> IS palindrome. ",line);
else
System.out.format("<%s> NOT palindrome. ",line);
}
} // END MAIN
// ******* MODIFY NOTHING ABOVE THIS LINE YOU FILL IN THE METHODS BELOW *******
// RETURNS A STRING WITH ALL NON ALPHABETIC CHARS REMOVED. ALL REMAINING ARE ALPHAS CONVERTED TO LOWER CASE
// "Madam I'm Adam" returns "madamimadam" which is now ready for a simple palindromic test
// To test whether a char is alpha i.e. letter of the alphabet
// read this ==> https://docs.oracle.com/javase/tutorial/i18n/text/charintro.html
static String toAlphaLowerCase( String s )
{
return ""; // (just to make it compile) YOU CHANGE AS NEEDED
}
// RETURNs true if and only if the string passed in is a palindrome
static boolean isPalindrome( String s )
{
return false; // (just to make it compile) YOU CHANGE AS NEEDED
}
} // END LAB3 CLASS
Explanation / Answer
Please find the modified code below.
CODE
====================
// "Madam I'm Adam" returns "madamimadam" which is now ready for a simple palindromic test
// To test whether a char is alpha i.e. letter of the alphabet
// read this ==> https://docs.oracle.com/javase/tutorial/i18n/text/charintro.html
public static String toAlphaLowerCase( String s )
{
String res = "";
for(char ch : s.toCharArray()) {
if (Character.isLetter(ch)) {
res += Character.toLowerCase(ch);
}
}
return res;
}
// RETURNs true if and only if the string passed in is a palindrome
public static boolean isPalindrome( String s )
{
int len = s.length();
for(int i = 0, j = len-1; i < len/2; i++, j--) {
if (s.charAt(i) != s.charAt(j))
return false;
}
return true;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.