From 61f6bfe694bb04c4e83e5f2110f3b5bf195e63e4 Mon Sep 17 00:00:00 2001 From: Deepika0908 <72943905+Deepika0908@users.noreply.github.com> Date: Fri, 16 Oct 2020 00:51:01 +0530 Subject: [PATCH] Create palindrome --- palindrome | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 palindrome diff --git a/palindrome b/palindrome new file mode 100644 index 0000000000..361f6ed0ff --- /dev/null +++ b/palindrome @@ -0,0 +1,39 @@ +package beginnersbook.com; +import java.util.Scanner; +class PalindromeCheck +{ + //My Method to check + public static boolean isPal(String s) + { // if length is 0 or 1 then String is palindrome + if(s.length() == 0 || s.length() == 1) + return true; + if(s.charAt(0) == s.charAt(s.length()-1)) + /* check for first and last char of String: + * if they are same then do the same thing for a substring + * with first and last char removed. and carry on this + * until you string completes or condition fails + * Function calling itself: Recursion + */ + return isPal(s.substring(1, s.length()-1)); + + /* If program control reaches to this statement it means + * the String is not palindrome hence return false. + */ + return false; + } + + public static void main(String[]args) + { + //For capturing user input + Scanner scanner = new Scanner(System.in); + System.out.println("Enter the String for check:"); + String string = scanner.nextLine(); + /* If function returns true then the string is + * palindrome else not + */ + if(isPal(string)) + System.out.println(string + " is a palindrome"); + else + System.out.println(string + " is not a palindrome"); + } +}