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
Abhishek Sharma 527Abhishek Sharma 527 

Specified index is invalid error in program

Hello there, I'm facing issue while executing this program, it says string exception Specified index is invalid for j variable
I believe it's declared and initialized properly,
//my code

//palindrome
String text = 'madam';
Integer i = 0;
Integer count = 0;
string res='';
Integer j = 0;

system.debug(res.length());
for(i=0,j=text.length()-1;i<=text.length();i++){
    if(text.charAt(i)==text.charAt(j)){
        j--;
        res = res + text.charAt(i);
        
    }
    else{
           break;
        system.debug('not matched');
    }
}
if(text==res)
    system.debug('palidrome');
else
    system.debug('not palidrome');

Can anyone please check if it's incorrect, it will be great help.
Best Answer chosen by Abhishek Sharma 527
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Abshishek,

For the code you shared there is small issue as highlited.
String text = 'madam';
Integer i = 0;
Integer count = 0;
string res='';
Integer j = 0;

system.debug(res.length());
for(i=0,j=text.length()-1;i<=text.length()-1;i++){
    if(text.charAt(i)==text.charAt(j)){
        j--;
        system.debug('sas'+text.charAt(i));
        res = res + text.charAt(i);
        system.debug('sas'+res);
    }
    else{
           break;
        system.debug('not matched');
    }
}
if(text==res)
    system.debug('palidrome');
else
    system.debug('not palidrome');

But the aboove program wont work because charAt will return equalent number for character but not character.

You can use the below code.
 
String temp= 'Madam';
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);
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Abshishek,

Are you trying to check Paidrome for string in apex?

Thanks,
 
Abhishek Sharma 527Abhishek Sharma 527
Thanks for response Sai, yes I'm checking Palindrome in apex.
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Abshishek,

For the code you shared there is small issue as highlited.
String text = 'madam';
Integer i = 0;
Integer count = 0;
string res='';
Integer j = 0;

system.debug(res.length());
for(i=0,j=text.length()-1;i<=text.length()-1;i++){
    if(text.charAt(i)==text.charAt(j)){
        j--;
        system.debug('sas'+text.charAt(i));
        res = res + text.charAt(i);
        system.debug('sas'+res);
    }
    else{
           break;
        system.debug('not matched');
    }
}
if(text==res)
    system.debug('palidrome');
else
    system.debug('not palidrome');

But the aboove program wont work because charAt will return equalent number for character but not character.

You can use the below code.
 
String temp= 'Madam';
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);
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
This was selected as the best answer