Question

In: Other

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.

Solutions

Expert Solution

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$ 

Related Solutions

A palindrome is a word or a phrase that is the same when read both forward and backward.
6.7 LAB: PalindromeA 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:bobthe output is:bob is a palindromeEx: If the input is:bobbythe output is:bobby is not a palindromeHint: Start by removing spaces. Then check if a string is equivalent to it's reverse.This is my code:s =...
A palindrome is a word or phrase, which reads the same backward or forward. Write a...
A palindrome is a word or phrase, which reads the same backward or forward. Write a program that prompts the user for a string of characters terminated by a period and determines whether the string (without the period) is a palindrome. IMP: Assume that the input contains only letters and blanks. Assume also that the input is at most 30 characters long. Use an array of characters of size 30 to store the input! Disregard blanks when deciding if the...
C++: A palindrome is a string that is the same backward as it is forward. For...
C++: A palindrome is a string that is the same backward as it is forward. For example, “tot” and “otto” are rather short palindromes. Write a program that lets a user enter a string and that passes to a bool function a reference to the string. The function should return true if the string is a palindrome and false otherwise. When you do the judgment, capitalization, spaces, and punctuation should be neglected, that is, “Madam, I’m Adam” should test as...
python question A word is a palindrome if it the same read forwards and backwards. We...
python question A word is a palindrome if it the same read forwards and backwards. We will call a word a fuzzy palindrome if it is the same read forwards and backwards, except for possible differences in case. For example, both 'tattarrattat' and 'TaTtArRAttat' are fuzzy palindromes. Define a function is_fuzzy_palindrome that returns True if and only if its argument is a fuzzy palindrome. This method may be useful: S.lower() -> str : Return a copy of the string S...
IN JAVA - [(1)] A palindrome is a string that reads the same forwards as backwards....
IN JAVA - [(1)] A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as appropriate [(2)] Let Q be a non-empty...
in java Read in a word and display the requested characters from that word in the...
in java Read in a word and display the requested characters from that word in the requested format. Write a Java program that ● prompts the user for a word and reads it, ● converts all characters of the input word to uppercase and display the word with a double quotation mark ( " ) at the start and end of the word, ● displays the word with all characters whose index is odd in lower case and for the...
A palindromic number reads the same both ways (left-to-right and right-to-left). The largest palindrome made from...
A palindromic number reads the same both ways (left-to-right and right-to-left). The largest palindrome made from the product of two 2-digit numbers is 9,009 = 91 × 99. The largest palindrome made from the product of two 3-digit numbers is 906,609 = 913 × 993. The largest palindrome made from the product of two 4-digit numbers is 99,000,099 = 9,901 × 9,999. 1. Write a function IN JAVASCRIPT to find the largest palindrome made from the product of two 7-digit...
the Elements of Style, read the following excerpts -- both from the same author, George Orwell,...
the Elements of Style, read the following excerpts -- both from the same author, George Orwell, and explain the different stylistic forms he uses in each. Excerpt from 1984 Power is not a means; it is an end. One does not establish a dictatorship in order to safeguard a revolution; one makes the revolution in order to establish the dictatorship. The object of persecution is persecution. The object of torture is torture. The object of power is power. Excerpt from...
What does Foot mean when she claims that the word good means the same thing when...
What does Foot mean when she claims that the word good means the same thing when applied to features of humans as when applied to features of plants?
Explain why when comparing futures rates to forward interest rates for the same tenor, a convexity...
Explain why when comparing futures rates to forward interest rates for the same tenor, a convexity adjustment is required. Explain why collateral requirements will increase in the OTC market as a result of new regulations introduced since the 2008 credit crisis.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT