Last active
January 17, 2018 07:20
-
-
Save Lauris01/32e9ad2cf28bcb37060ab33adb8e7e19 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main { | |
public static void main(String[] args) { | |
System.out.println(isPalindrome("A man, a plan, a canal - Panama!")); | |
} | |
/** | |
* Method to check if given string is a palindrome | |
* @param sentence - sentence | |
* @return boolean - if sentence is a palindrome | |
*/ | |
public static boolean isPalindrome(String sentence){ | |
sentence = sentence.replaceAll("[^A-Za-z0-9]", ""); | |
sentence = sentence.toLowerCase(); | |
int start = 0; | |
int end = sentence.length() - 1; | |
while(start <= end){ | |
if (sentence.charAt(start) == sentence.charAt(end)){ | |
start ++; | |
end --; | |
} else { | |
return false; | |
} | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment