Groovy 35- How To Check if a String is a Palindrome
From Wiki:
A palindrome is a word, phrase, number, or other sequence of characters that reads the same backward as forward, such as madam or racecar. Sentence-length palindromes may be written when allowances are made for adjustments to capital letters, punctuation, and word dividers, such as "A man, a plan, a canal, Panama!", "Was it a car or a cat I saw?" or "No 'x' in Nixon".
Composing literature in palindromes is an example of constrained writing.
The word "palindrome" was coined by the English playwright Ben Jonson in the 17th century from the Greek roots palin ("again") and dromos ("way, direction").
Source: Wikipedia
Checking for a Palindrome in Groovy
Let's say we have the string racecar. Here's how to write a script to check if this string is a palindrome:
// First, declare the string variable def word = "racecar" // Declare variables to hold the reversed string and the result def checkDone String reverse = "" // Get the size of the word int length = word.size() // Reverse the word for (int i = length - 1; i >= 0; i--) { reverse += word.charAt(i) } // Check if the original word is equal to the reversed word if (word.equalsIgnoreCase(reverse)) { checkDone = "The string '" + word + "' is a palindrome" } else { checkDone = "The string '" + word + "' is not a palindrome" } // Print the result println(checkDone)