Java palindrome case sentitive (#214)

* add palindrome check function with case sensitive

* rename file to fix dead link

* fix code spell
pull/216/head
Toihir Halim 2021-04-16 16:08:21 +02:00 committed by GitHub
parent 4eb07f8982
commit 215443893b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 4 deletions

View File

@ -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");
}
}