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

Answer :

ammary456

Answer:

string = input()

normal = ""

reverse = ""

for i in range(len(string)):

   if string[i].isalpha():

       normal += string[i].lower()

       reverse = string[i].lower() + reverse

if normal == reverse:

   print(string + " is a palindrome")

else:

   print(string + " is not a palindrome")

Explanation:

  • Loop up to the length of string.
  • Use an if statement to check if a certain character is an alphabet.
  • Check if both the normal and reverse strings are equal, then display that it is a palindrome.
frknkrtrn

The code below is in Java

It checks if a given String is a palindrome or not. The steps of the algorithm are:

  1. Remove the spaces from the word using replaceAll() method and assign it to the spaceRemovedWord
  2. Reverse the word using a for loop and assign it to the reversedWord
  3. Check if the reversedWord and spaceRemovedWord are equal using if-else structure

Comments are used to explain the each line.

//Main.java

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

    //declare the Scanner object to get input

    Scanner input = new Scanner(System.in);

    //declare the variables

    String word, spaceRemovedWord, reversedWord = "";

   

    //get the input

    word = input.nextLine();

    //remove all the spaces

    spaceRemovedWord = word.replaceAll("\\s+","");

       

       //create a for loop that iterates from end of the string to the front

       //  and appending each character to the reversedWord to reverse the word

    for(int i=spaceRemovedWord.length()-1; i>=0; i--)

              reversedWord += spaceRemovedWord.charAt(i);

       

       //check if the spaceRemovedWord and reversedWord are equal

       //if they are, it is a palindrome

       //otherwise, it is not

       if(spaceRemovedWord.equals(reversedWord))

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

       else

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

 

}

}

You may check another String question at:

brainly.com/question/15061607

Other Questions