You need to sign in to do that
Don't have an account?
Abhishek 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.
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.
For the code you shared there is small issue as highlited.
But the aboove program wont work because charAt will return equalent number for character but not character.
You can use the below code.
Let me know if you face any issues.
If this solution helps, Please mark it as best answer.
Thanks,
All Answers
Are you trying to check Paidrome for string in apex?
Thanks,
For the code you shared there is small issue as highlited.
But the aboove program wont work because charAt will return equalent number for character but not character.
You can use the below code.
Let me know if you face any issues.
If this solution helps, Please mark it as best answer.
Thanks,