Skip to content

Instantly share code, notes, and snippets.

@Lauris01
Last active January 17, 2018 07:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lauris01/32e9ad2cf28bcb37060ab33adb8e7e19 to your computer and use it in GitHub Desktop.
Save Lauris01/32e9ad2cf28bcb37060ab33adb8e7e19 to your computer and use it in GitHub Desktop.
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