Question

A palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g.,...

A palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run.

In this program, ask the user to input some text and print out whether or not that text is a palindrome.

Create the Boolean method isPalindrome which determines if a String is a palindrome, which means it is the same forwards and backwards. It should return a boolean of whether or not it was a palindrome.

Create the method reverse which reverses a String and returns a new reversed String to be checked by isPalindrome.

Both methods should have the signature shown in the starter code.

Sample output:

Type in your text: 
madam
Your word is a palindrome!

OR

Type in your text: 
hello
Not a palindrome :(

Coding Portion:

public class Palindromes
{
/**
* This program lets the user input some text and
* prints out whether or not that text is a palindrome.
*/
public static void main(String[] args)
{
// Create user input and let user know whether their word is a palindrome or not!
  
}
  
/**
* This method determines if a String is a palindrome,
* which means it is the same forwards and backwards.
*
* @param text The text we want to determine if it is a palindrome.
* @return A boolean of whether or not it was a palindrome.
*/
public static boolean isPalindrome(String text)
{
// Your code goes here!
}
  
/**
* This method reverses a String.
*
* @param text The string to reverse.
* @return The new reversed String.
*/
public static String reverse(String text)
{
// Your code goes here!
}

}

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

Code:

Output:

Raw Code:

import java.util.*; // Importing required packages
public class Palindromes{
   public static void main(String[] args) {
       System.out.println("Type in your text:");
       Scanner scan = new Scanner(System.in); // Creating Scanner instance for taking input from user.
       String str = scan.next(); // Taking string from user and store it in str variable.
       if (isPalindrome(str)){ // Checking if a str is palindrome or not.
           System.out.println("Your word is a palindrome!");
       }
       else{ // If it is not a palindrome
           System.out.println("Not a palindrome :(");
       }
   }
   // isPalindrome function for checking if a given string is palindrome or not.
   public static boolean isPalindrome(String text){
       String reverse_text = reverse(text); // Calling reverse function with text as a parameter.
       if (text.equals(reverse_text)){ // Checking if a text and reverse_text are equal or not
           return true; // Returning true
       }
       else{
           return false; // Returning false
       }
   }

   public static String reverse ( String text){
       String rev = ""; // Declaring rev variable.
       for (int i = text.length() -1;i>=0;i--){ // Iterate through each character in text in reverse order.
           rev += text.substring(i,i+1); // adding each character to rev.
       }
       return rev; // Returning the rev.
   }
}


Note : If you have any doubts plz ask me in comment section.

Add a comment
Know the answer?
Add Answer to:
A palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g.,...
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...

  • 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;...

  • Complete the printPalindrome method in the provided Palindrome.java program. The printPalindrome method accepts a String as...

    Complete the printPalindrome method in the provided Palindrome.java program. The printPalindrome method accepts a String as a parameter and prints whether the parameter String is a palindrome (i.e., reads the same forwards as it does backwards, for example, "abba" or "racecar"). Make the code case-insensitive, so that words like "Abba" and "Madam" will be considered palindromes. import java.util.*; /** * Determines if a user entered String is a palindrome, which means the String * is the same forward and reverse....

  • 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....

  • C++ A palindrome is a string that reads the same backward as forward. For example, the...

    C++ A palindrome is a string that reads the same backward as forward. For example, the words mom, dad, madam and radar are all palindromes. Write a class Pstring that is derived from the STL string class. The Pstring class adds a member functionbool isPalindrome( )that determines whether the string is a palindrome. Include a constructor that takes an STL string object as parameter and passes it to the string base class constructor. Test your class by having a main...

  • 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...

  • 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...

  • 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 a number or a text phrase that reads the same backward as forward....

    A palindrome is a number or a text phrase that reads the same backward as forward. For example, each of the following six-digit integers is a palindrome: 123321, 555555, 455554, and 116611. Write a pseudocode and C++ program that reads in a six-digit integer and determines whether or not it is a palindrome. You must use the division and remainder operators. Assume input is correct (six-digit integer). Please don't use loops. Do it with only using if else.

  • C++ A palindrome is a string that reads the same backward as forward. For example, the words mom, dad, madam and radar are all palindromes. Write a class Pstring that is derived from the STL string cl...

    C++ A palindrome is a string that reads the same backward as forward. For example, the words mom, dad, madam and radar are all palindromes. Write a class Pstring that is derived from the STL string class. The Pstring class adds a member functionbool isPalindrome( )that determines whether the string is a palindrome. Include a constructor that takes an STL string object as parameter and passes it to the string base class constructor. Test your class by having a main...

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