forked from rakhi2207/java-programs-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
palindrome.java
66 lines (50 loc) · 1.64 KB
/
palindrome.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.io.*;
public class Palindrome{
public static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
//=========================methods======================
public static boolean isFiveDigit(String strDigit){
if(strDigit.length()==5)
return true;
return false;
}//test if the input number is five digit
public static String stringReverse(String rStr){
StringBuffer strBuff = new StringBuffer(rStr);
return strBuff.reverse().toString();
}//reverse the string
public static String removeLetter(String str){
int i,len = str.length();
StringBuffer sBuf = new StringBuffer(str);
char c;
for(i= (len-1);i>=0;--i){
c = sBuf.charAt(i);
if(!Character.isDigit(c))
sBuf.append(c);
}
return sBuf.toString();
}//remove the letter in the digit
public static void isPalindrome(String strToTest,String revStr){
if(revStr.equalsIgnoreCase(strToTest)){
System.out.println("\n" + revStr + " is a palindrome!...\n");
}else{
System.out.println("\n" + revStr+" is not a Palindrome!.\n");
}
}//check if the digit is a palindrome
//=======================main=================================
public static void main(String []args)throws IOException{
String str,reverseStr;
int tag;
do{
System.out.print("\nEnter a five-digit integers: ");
str = input.readLine();
str = removeLetter(str);
if(!isFiveDigit(str)){
System.out.print("\nInput number is not a five-digit integers");
tag=1;
}else{
tag=0;
}
}while(tag==1);//loop until user input is correct
reverseStr = stringReverse(str);
isPalindrome(str,reverseStr);
}//end of main
}//end of class