There might a lot of resources out in the web to determine if a string is palindrome using Javasript, C#, Java and other programming languages. I took a step-back and thought of sharing the code in Apex to help.
Approach – 1: Visualforce Page + Apex
Visualforce Page
<apex:page controller="PalindromeController" docType="html-5.0">
<apex:form>
<apex:inputText value="{!palindromeTxt}" label="Enter input" />
<apex:commandButton value="Check Palindrome" action="{!checkPalindrome}" />
<div style="font-weight: 25px;">{!palindromeResult}</div>
</apex:form>
</apex:page>
Apex controller
public with sharing class PalindromeController {
// Input VF variable to get the text
// from user
public String palindromeTxt {get;set;}
// Output VF variable to show the result
public String palindromeResult {get;set;}
public PageReference checkPalindrome () {
// Initialize list to store the reverse
// characters
List<String> reverseLst = new List<String>();
// In order to make it easier, when remove
// blanks and any whitespaces from the string,
// and also convert all into lowercase string.
String txtToConvert = palindromeTxt.toLowerCase().deleteWhitespace();
List<String> normalLst = txtToConvert.split('');
// Once the characters are split into List
// we remove special characters and spaces
// from the user string.
txtToConvert = txtToConvert.replaceAll('[^a-zA-Z0-9\\s+]', '');
// Iterate through the suser input List generated above
// and store the characters in reverse order
for(Integer i = normalLst.size()-1; i>=0;i--) {
reverseLst.add(normalLst.get(i));
}
// Once the List is reversed,
// we remove special characters and spaces
// from the user string.
String reverseLstString = String.join(reverseLst, '').replaceAll('[^a-zA-Z0-9\\s+]', '');
// We compare the user string and the reversed string
// if they match then we consider it as palindrome.
if( txtToConvert == reverseLstString ) {
palindromeResult = 'It is a palindrome';
} else {
palindromeResult = 'It is not a palindrome';
}
return null;
}
}
Approach – 2: Visualforce Page + JS
<apex:page controller="PalindromeController" docType="html-5.0">
<script>
function checkPalindrome() {
let inputStr = document.getElementById('inputLabel').value.toLowerCase();
inputStr = inputStr.replace(/[^a-zA-Z0-9]/g, '');
let output;
let stringArr = inputStr.split('');
stringArr = stringArr.filter(function(entry) { return entry.trim() != ''; });
let reverseStr = stringArr.reverse().join('').toLowerCase();
if( inputStr == reverseStr ) {
output = "It is a palindrome";
} else {
output = "It is not a palindrome";
}
document.getElementById('outputString').innerHTML = output;
}
</script>
<input type="text" id="inputLabel" value="" />
<button onclick="checkPalindrome()">Check Palindrome</button>
<div id="outputString" style="font-weight: 25px;"></div>
</apex:page>