Question

This is a java question A palindrome is a word or phrase that reads the same...

 
This is a java question
A palindrome is a word or phrase that reads the same forward and backward, ignoring blanks and punctuations, and considering uppercase and lowercase versions of the same letter to be equal. For example, the following are palindromes:

warts n straw
radar
Able was I ere I saw Elba
tacocat

Write a program named Palindrome.java that will accept a file (file name) from the command argument list and decide whether each line in the file is a palindrome. Then output the string of each line, followed by True if the string is a palindrome and False otherwise.

Note: data file palindrome.txt is provided for testing purpose. You may use this file to text your program.

Sample output:
Able was I ere I saw Elba       True
warts n straw   True
This is not a palindrome!  False
Java      False
0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Palindrome {

    public static boolean isPalindrome(String str) {
        String modified = "";
        for(int i = 0; i < str.length(); ++i) {
            if(Character.isAlphabetic(str.charAt(i))) {
                modified += Character.toLowerCase(str.charAt(i));
            }
        }
        for(int i = 0; i < modified.length(); ++i) {
            if(modified.charAt(i) != modified.charAt(modified.length()-i-1)) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        if(args.length == 0) {
            System.out.println("Error. command line argument for file name is not provided.");
            System.exit(-1);
        }
        File file = new File(args[0]);
        try {
            Scanner fin = new Scanner(file);
            String line;
            while (fin.hasNextLine()) {
                line = fin.nextLine();
                if(isPalindrome(line)) {
                    System.out.println(line + "\tTrue");
                } else {
                    System.out.println(line + "\tFalse");
                }
            }
            fin.close();
        } catch (FileNotFoundException e) {
            System.out.println(file.getAbsolutePath() + " does not exists!");
        }
    }

}

Able was I ere I saw Elba True warts n straw True This is not a palindrome! False Java False Process finished with exit code O

\color{red} Please\;let\;me\;know\;if\;you\;need\;me\;to\;make\;any\;changes...

Add a comment
Know the answer?
Add Answer to:
This is a java question A palindrome is a word or phrase that reads the same...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A palindrome is a word or phrase that reads the same forward and backward, ignoring blanks...

    A palindrome is a word or phrase that reads the same forward and backward, ignoring blanks and considering uppercase and lowercase versions of the same letter to be equal. For example, the following are palindromes: • warts n straw • radar • Able was I ere I saw Elba • tacocat Write a program that will accept a sequence of characters terminated by a period and will decide whether the string—without the period—is a palindrome. You may assume that the...

  • C++ Programming Palindrome Detector: A palindrome is any word, phrase, or sentence that reads the same...

    C++ Programming Palindrome Detector: A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here are some well-known palindromes: Able was I, ere I saw Elba A man,a plan, a canal, Panama Desserts, I stressed Kayak Write a book function that uses recursion to determine if a string argument is a palindrome. The function should return true if the argument reads the same forward and backward. Demonstrate the function in a program, which continues to...

  • Palindrome Detector C++ A palindrome is any word, phrase, or sentence that reads the same forward...

    Palindrome Detector C++ A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here are some well-known palindromes: Able was I, ere I saw Elba A man, a plan, a canal, Panama Desserts, I stressed Kayak Write a program that determine whether an input string is a palindrome or not. Input: The program should prompt the user "Please enter a string to test for palindrome or type QUIT to exit: " and then wait for...

  • Using C language Palindromes A palindrome is a word or phrase that reads the same backwards...

    Using C language Palindromes A palindrome is a word or phrase that reads the same backwards and forwards, ignoring punctuation, spaces, and anything that isn’t a letter. Examples of Palindromes Madam, I’m Adam. Kayak Racecar A man, a plan, a canal – Panama! Able was I, ere I saw Elba. Go hang a salami, I’m a lasagna hog! Write a program that does the following: opens a text file named “Proj3input.txt” containing multiple lines of text (1 or more) for...

  • A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here...

    A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here are some well-known palindromes: Able was I, ere I saw Elba A man, a plan, a canal, Panama Desserts, I stressed Kayak Write a program that determine whether an input string is a palindrome or not. Input: The program should prompt the user "Please enter a string to test for palindrome or type QUIT to exit: " and then wait for the user input....

  • Problem 1: (Palindromes) A palindrome is a string that's spelled the same way forward and backward....

    Problem 1: (Palindromes) A palindrome is a string that's spelled the same way forward and backward. Some examples of palindromes are; radar, able was i ere i saw elba; and, if you ignore blanks, a man a plan a canal panama .Write a recursive function testPa1indrome that returns 1 if the string stored in the array is a palindrome and 0 otherwise. The function should ignore spaces and punctuation in the string. Please I need it in C program and...

  • C Program: 6.31 (Palindromes) A palindrome is a string that's spelled the same way forward and...

    C Program: 6.31 (Palindromes) A palindrome is a string that's spelled the same way forward and backward. Some examples of palindromes are: "radar" "able was i ere i saw elba" and, if you ignore blanks: "a man a plan a canal panama" Write a recursive function testPalindrome that returns 1 if the string stored in the array is a palindrome and 0 otherwise. The function should ignore spaces and punctuation in the string. Use the function in a complete program...

  • A palindrome is a word or phrase that reads the same both forwards and backwards. "racecar"...

    A palindrome is a word or phrase that reads the same both forwards and backwards. "racecar" is a palindrome. So is, "a man a plan a canal, panama" if you ignore the spacing and the comma. Discovering how to automate the detection of palindromes is a good way to test your understanding of loops and strings. The file, "hw2problem5.py", contains a function called is_palindrome(), which has a single string parameter, s. If s is a palindrome, then is_palindrome() returns the...

  • A palindrome is a word, phrase, number, or other sequence of characters which reads the same...

    A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward, such as madam or racecar. Write a recursive method in java that accepts a integer as its argument and returns true if it is palindrome, false otherwise. public class Recursion {                 public static void main(String[] args) {                                 Recursion r = new Recursion();                                 System.out.println(“Is 12321 a palindrome? “+ra.isPalindrome(12321)); //true                 }                 public Boolean isPalindrome(int num){                                 return false;...

  • Please Code in Java and follow the directions given Thanks Program required Directions .A palindrome is a str...

    Please Code in Java and follow the directions given Thanks Program required Directions .A palindrome is a string that reads the same forward and backward, i.e., the letters are the same whether you read them from right to left or from left to right. Examples: 3 a) radar à is a palindrome b)Able was I ere I saw Elba à is a palindrome e good à not a palindrome Write java program to read a line of text and tell...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT