From 215443893be3d45594c5e4e8d6ddb08a8338103c Mon Sep 17 00:00:00 2001 From: Toihir Halim <66559721+toihirhalim@users.noreply.github.com> Date: Fri, 16 Apr 2021 16:08:21 +0200 Subject: [PATCH] Java palindrome case sentitive (#214) * add palindrome check function with case sensitive * rename file to fix dead link * fix code spell --- .../java/{paindrome.java => palindrome.java} | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) rename strings/java/{paindrome.java => palindrome.java} (54%) 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"); } }