Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this assignment do not worry about error checking any of the inputs. As usua

ID: 3857218 • Letter: F

Question

For this assignment do not worry about error checking any of the inputs. As usual, all displayed prompts and results should be well formatted This program will identify palindrome and non-palindrome words in a text file named words.txt. The programmer will create the file with each line having one word. The words in the file may or may not be palindromes. Use google to look up palindromes to put in the file and also include non-palindromes in the file. Upper and lower case characters are considered to be a match for palindrome analysis. This program must use the Java String Classes and related classes. Console display control must be well formatted. The program should display the result using the original word in a statement that indicates if it is a palindrome or not, such as: Dad is a palindrome. Apple is not a palindrome. I is a palindrome. When the file has been processed, the program should display basic statistics on how many words were processed how many were palindromes and how many were not palindromes.

Explanation / Answer

import java.io.*;

import java.util.*;

public class chegg_palindrome {

public static void main(String[] args) throws Exception{

BufferedReader reader = new BufferedReader(new FileReader("words.txt"));

String s,temp,temp_reverse;

Integer count_of_words=0;

Integer count_of_palindrome=0;

while((s = reader.readLine())!=null)

{

temp=s;

temp=temp.toLowerCase();

temp_reverse=new StringBuilder(temp).reverse().toString();

count_of_words=count_of_words+1;

if(temp.equals(temp_reverse))

{

System.out.println(s+" is a palindrome");

count_of_palindrome=count_of_palindrome+1;

}

else

{

System.out.println(s+" is not a palindrome");

}

}

System.out.println("Count of Number of words processed "+ count_of_words );

System.out.println("Count of Number of Palindromes "+ count_of_palindrome );

System.out.println("Count of Number of Non Palindromes "+ (count_of_words-count_of_palindrome) );

reader.close();

}

}

words.txt:

Apple
Dad
I

Sample Output:

Apple is not a palindrome
Dad is a palindrome
I is a palindrome
Count of Number of words processed 3
Count of Number of Palindromes 2
Count of Number of Non Palindromes 1

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote