Create a program reads in strings of characters


Discussion:

Q: Write a program that reads in strings of characters and determines if each string is a palindrome. Each string appears on a separate input line. Echo-print each string, followed by " Is a palindrome" if the string is a palindrome or "Is not a palindrome" if the stringis not a palindrome. For example, given the input string Able was, I ere I saw Elba. The program should print "Is a palindrome." In detemining whether a string is a palindrome, consider uppercase and lower case letters to be the same and ignore punctuation characters and spares. The input file that you will be reading palindromes from is called "pals.txt". Look for methods like replace (), to lowerCase, to UpperCase,..etc. Write the output ( the string and if is a palindrome) to a file called "palsout.txt". Each line of text in the file pals.txt will contain a palindrome to test for. You can only use the BufferedReader class and not the Scanner class. import java.io.*;

public class PalindromeTester
{
public static void main (String [] args) throws IOException

{
BufferedReader inFile = new BufferedReader (new FileReader ("pals.txt"));
String line;
line = inFile.readLine(); //initialize
String str, another = "y";
int left, right;
String newString = "";

while(line != null) // boolean expression

{
System.out.println(line);
line = inFile.readLine(); //read the next line

}

PrintWriter outFile = new PrintWriter (new FileWriter("output.txt"));
outFile.println("The string and if is a palindrome");
inFile.close();
outFile.close();

while (another.equalsIgnoreCase("y")) // allows y or Y
{
str = inFile.readLine();
left = 0;
right = str.length() - 1;


while (str.charAt(left) == str.charAt(right) && left < right)
{
left++;
right--;

}

System.out.println();

if (left < right)
System.out.println ("Is NOT a palindrome.");

else
System.out.println ("Is a palindrome.");
System.out.println();
another = inFile.readLine();

}

}
}

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Create a program reads in strings of characters
Reference No:- TGS01932328

Now Priced at $20 (50% Discount)

Recommended (96%)

Rated (4.8/5)