function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ritik DwivediRitik Dwivedi 

how we write apex programme to check string is palindrome or not ? please solve my problem

Arvind_SinghArvind_Singh
Hello Ritik

You need to reverse string and check if both are same. 

In apex you have Reverse function for it 
 
String str = 'Arvind';
String revStr = str.reverse();
system.debug('Reversed string value is ' + revStr);
if (str== revStr ) 
{  system.debug('String is Palindrom :'+revStr ); 
}else 
{ 
system.debug('String is not Palindrom :'+revStr ); 
}

without function
String temp= 'Civic';
integer stringLength=0;
stringLength=temp.Length();
string revString='';
for (Integer i=temp.Length()-1;i>=0;i--) {
    system.debug('Place of char' + I + '---'+ (i+1));
    revString = revString+ temp.substring(i,i+1) ;
    system.debug(revString);
}
if (temp == revString) {
    system.debug('String is Palindrom :'+revString);
}else {
    system.debug('String is not Palindrom :'+revString);
}

 
Ankit PurohitAnkit Purohit
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.

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.
Shreya GhoderaoShreya Ghoderao
Best Answer
Yash BhutaniYash Bhutani
@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.
Gaurav Misar 2Gaurav Misar 2
variable initialisation