Specification: A palindrome is a string that is the same when reversed. For exam
ID: 3820544 • Letter: S
Question
Specification: A palindrome is a string that is the same when reversed. For example, "abba" is a palindrome. Here is a math-like definition: palindrome(" ") = true palindrome (x) = true palindrome (x + X + y) = false, if x ! = y = palindrome (X), if x = = y The symbol x stands for a single character, as does y. The symbol X stands for a string of characters. The symbol + stands for concatenation. In this version of palindrome every character must be matched. So punctuation and whitespace on one side of a string must match the same on the other. Also (for us) the case of a character must match. Implement palindrome () by following the above definition and write a main () that tests it public static boolean palindrome (String word) Design: Make this a console application, i e. a class that contains a static main that asks the user for a possible palindrome, reads in a complete line, and then uses palindrome () to test it. Write out the results. You only need to ask the user for one possible palindrome. Of course, palindrome () must be implemented recursively as above. Simple l/O is enough. You don't need to use Exceptions. You will need several methods of the String class. A run of the program: Enter a string: ABCBA That is a palindrome Another run: Enter a string: Radar That is NOT a palindrome Another run: Enter a string: able was I ere I saw elba That is a palindromeExplanation / Answer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package palindrom;
import java.util.Scanner;
/**
*
* @author KONDA REDDY
*/
public class Palindrom {
/**
* @param args the command line arguments
*/
public static boolean Palindrom(String str)
{
String reverse="";
int len=str.length();
for(int i=len-1;i>-1;i--)
{
reverse=reverse+str.charAt(i);
}
if(str.equals(reverse))
{
return true;
}else{
return false;
}
}
public static void main(String[] args) {
String str;
String reverse="";
Scanner sc = new Scanner(System.in);
System.out.print("Enter your String which u want to check : ");
str = sc.nextLine();
boolean b1=Palindrom(str);
if(b1==true)
System.out.println("That is a Palindrome");
else
System.out.println("That String is not a Palindrome");
}
private String charAt(int i) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.