diff --git a/strings/java/paindrome.java b/strings/java/palindrome.java similarity index 54% rename from strings/java/paindrome.java rename to strings/java/palindrome.java index f8a8cb21..eafe646d 100644 --- a/strings/java/paindrome.java +++ b/strings/java/palindrome.java @@ -1,4 +1,4 @@ -public class Algorithm { +public class palindrome { // Function that returns true if // str is a palindrome @@ -25,15 +25,32 @@ public class Algorithm { // Given string is a palindrome return true; } + + //new approach with case sensitive + public static boolean isPalindrome(String str, boolean caseSensitive){ + // lowercase if not case sensitive + if(!caseSensitive) str = str.toLowerCase(); + + for(int i = 0; i < str.length() / 2; i++) + if(str.charAt(i) != str.charAt(str.length() - 1 - i)) + return false; + + return true; + } // Driver code public static void main(String[] args) { - String str = "geeks"; + String str = "Dad"; if (isPalindrome(str)) - System.out.print("Yes"); + System.out.println("Yes"); else - System.out.print("No"); + System.out.println("No"); + + if (isPalindrome(str, false)) + System.out.println("Yes"); + else + System.out.println("No"); } }