File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ package beginnersbook.com;
2
+ import java.util.Scanner;
3
+ class PalindromeCheck
4
+ {
5
+ //My Method to check
6
+ public static boolean isPal(String s)
7
+ { // if length is 0 or 1 then String is palindrome
8
+ if(s.length() == 0 || s.length() == 1)
9
+ return true;
10
+ if(s.charAt(0) == s.charAt(s.length()-1))
11
+ /* check for first and last char of String:
12
+ * if they are same then do the same thing for a substring
13
+ * with first and last char removed. and carry on this
14
+ * until you string completes or condition fails
15
+ * Function calling itself: Recursion
16
+ */
17
+ return isPal(s.substring(1, s.length()-1));
18
+
19
+ /* If program control reaches to this statement it means
20
+ * the String is not palindrome hence return false.
21
+ */
22
+ return false;
23
+ }
24
+
25
+ public static void main(String[]args)
26
+ {
27
+ //For capturing user input
28
+ Scanner scanner = new Scanner(System.in);
29
+ System.out.println("Enter the String for check:");
30
+ String string = scanner.nextLine();
31
+ /* If function returns true then the string is
32
+ * palindrome else not
33
+ */
34
+ if(isPal(string))
35
+ System.out.println(string + " is a palindrome");
36
+ else
37
+ System.out.println(string + " is not a palindrome");
38
+ }
39
+ }
You can’t perform that action at this time.
0 commit comments