Answer :
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.
The code below is in Java
It checks if a given String is a palindrome or not. The steps of the algorithm are:
- Remove the spaces from the word using replaceAll() method and assign it to the spaceRemovedWord
- Reverse the word using a for loop and assign it to the reversedWord
- 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