Skip to content

Instantly share code, notes, and snippets.

@rjlutz rjlutz/Palindrome Utility
Last active Aug 15, 2018

Embed
What would you like to do?
Bits to build a palindrome app, class with static method that tests a string to see if its a palindrome
public class Palindrome {
public static boolean check(String _s) {
return check(_s, 0, _s.length()-1);
}
private static boolean check(String _s, int _start, int _end) {
// base case
if (_start >= _end) return true;
// do some work
while(!Character.isLetterOrDigit(_s.charAt(_start)))
(_start)++;
while(!Character.isLetterOrDigit(_s.charAt(_end)))
_end--;
char downcaseStart = Character.toLowerCase(_s.charAt((_start)));
char downcaseEnd = Character.toLowerCase(_s.charAt(_end));
if (downcaseStart != downcaseEnd) return false;
// call simpler version
return check(_s, (_start)+1, _end-1);
}
}
TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
String entered = input.getText().toString();
if (Palindrome.check(entered)) {
Log.v(TAG, "is a palindrome");
result.setImageResource(R.drawable.check);
} else {
Log.v(TAG, "is not a palindrome");
result.setImageResource(R.drawable.x);
}
}
};
input.addTextChangedListener(textWatcher);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.