Salesforce has a reverse() function and it makes easy to comapare strings.
1. Take the string as input parameter. 2. Take a String variable, lets say reverseString. 3. Now reverse the input string using reverse() function and assign it to reverseString variable. 4. Now check if input parameter string is equal to reverseString variable or not. If it matches then it is a palindrome if not then they are not palindrome. Please check my code below for further reference.
@ankitpurohit 'Method does not exist or incorrect signature: void palindrome() from the type reversestring" getting this error while trying to execute this code, please solve my problem.
You need to reverse string and check if both are same.
In apex you have Reverse function for it
without function
1. Take the string as input parameter.
2. Take a String variable, lets say reverseString.
3. Now reverse the input string using reverse() function and assign it to reverseString variable.
4. Now check if input parameter string is equal to reverseString variable or not. If it matches then it is a palindrome if not then they are not palindrome. Please check my code below for further reference.
public class myPalindrome {
public void palindrome(String inputString){
String reverseString ='';
reverseString = inputString.reverse();
if(inputString.equals(reverseString)){
system.debug('String is Palindrome');
}
else{
system.debug('String is not Palindrome');
}
}
}
Please mark this answer if it solves your query.