Question

JAVA. A palindrome is a word or a phrase that is the same when read both forward and backward.

java please. 

A palindrome is a word or a phrase that is the same when read both forward and backward. Examples are: "bob," "sees," or "never odd or even" (ignoring spaces). Write a program whose input is a word or phrase, and that outputs whether the input is a palindrome.

Ex: If the input is:

bob

the output is:

bob is a palindrome

Ex: If the input is:

bobby

the output is:

bobby is not a palindrome

Hint: Start by removing spaces. Then check if a string is equivalent to it's reverse.


Hint: Start by just handling single-word input, and submit for grading. Once passing single-word test cases, extend the program to handle phrases. If the input is a phrase, remove or ignore spaces.

0 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1

If you will feel any problem with the solution, then feel free to ask it in comments.

Happy HomeworkLibing

LabProgram1.java

public class LabProgram1 {
    public static void main(String[] args) {
        print_palindrome_or_not("bob");
        print_palindrome_or_not("bobby");
        print_palindrome_or_not("sees");
        print_palindrome_or_not("never odd or even");
    }

    public static void print_palindrome_or_not(String line) {
        if (isPalindrome(line)) {
            System.out.println(line + " is a palindrome");
        } else {
            System.out.println(line + " is not a palindrome");
        }
    }

    public static boolean isPalindrome(String line) {
        int i=0;
        int j = line.length()-1;
        while (i < j) {
            if (line.charAt(i) == line.charAt(j)) {
                ++i;
                --j;
            } else if (line.charAt(i) == ' ') {
                ++i;
            } else if (line.charAt(j) == ' ') {
                --j;
            } else {
                return false;
            }
        }
        return true;
    }
}

OUTPUT

(base) avianjjai@avianjjai-Vostro-3578:~/Desktop/Chegg$ java LabProgram1.java
bob is a palindrome
bobby is not a palindrome
sees is a palindrome
never odd or even is a palindrome
(base) avianjjai@avianjjai-Vostro-3578:~/Desktop/Chegg$ 

LabProgram1.js

function isPalindrome(line) {
    var i=0;
    var j = line.length-1;
    while (i < j) {
        if (line.charAt(i) == line.charAt(j)) {
            ++i;
            --j;
        } else if (line.charAt(i) == ' ') {
            ++i;
        } else if (line.charAt(j) == ' ') {
            --j;
        } else {
            return false;
        }
    }
    return true;
}

function print_palindrome_or_not(line) {
    if (isPalindrome(line)) {
        console.log(line + " is a palindrome");
    } else {
        console.log(line + " is not a palindrome");
    }
}

print_palindrome_or_not('bob');
print_palindrome_or_not('bobby');
print_palindrome_or_not('sees');
print_palindrome_or_not('never odd or even');

OUTPUT

(base) avianjjai@avianjjai-Vostro-3578:~/Desktop/Chegg$ node LabProgram1.js
bob is a palindrome
bobby is not a palindrome
sees is a palindrome
never odd or even is a palindrome
(base) avianjjai@avianjjai-Vostro-3578:~/Desktop/Chegg$ 
Add a comment
Know the answer?
Add Answer to:
JAVA. A palindrome is a word or a phrase that is the same when read both forward and backward.
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
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
ADVERTISEMENT