/// Returns only the letters and digits of a text, in lowercase.
String cleanText(String text) {
    String lower = text.toLowerCase();
    String result = "";
    for (int i = 0; i < lower.length(); i++) {
        char c = lower.charAt(i);
        if (Character.isLetterOrDigit(c)) {
            result += c;
        }
    }
    return result;
}

/// Returns the text backward.
String reverse(String text) {
    String reversed = "";
    for (int i = text.length() - 1; i >= 0; i--) {
        reversed += text.charAt(i);
    }
    return reversed;
}

/// Returns true if the text reads the same forward and backward.
boolean isPalindrome(String text) {
    String cleaned = cleanText(text);
    return cleaned.equals(reverse(cleaned));
}

void main() {
    String[] tests = {
        "racecar",
        "A man, a plan, a canal: Panama",
        "Was it a car or a cat I saw?",
        "hello world"
    };

    for (String test : tests) {
        IO.println(isPalindrome(test) + "  " + test);
    }
}

// From Groundwork, chapter 8 (ch08-strings-as-data), box 17.
// This example is from Groundwork, licensed CC BY-NC-SA 4.0.
// Run with: java ch08_17.java
