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
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 "); // 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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started