Please explain my mistake

class Solution {
public:
bool isPalindrome(string s) {
//for (int i=0; i<s.length(); i++)
//tolower(s[i]);
int i=0,j= s.length()-1;
while(i<j){
if(!isalnum(s[i]))
i++;
else if(!isalnum(s[j]))
j–;
if(tolower(s[i])==tolower(s[j])){
i++;
j–;
}
else{
return false;
}
}
return true;
}
};

@Prabhleen_sheenu consider a case when there are more than 1 consecutive non alphanumeric characters (eg:", ") here both comma and space needs to be ignored but you only ignored comma
So use while instead of if.

Ohh.sir i added one more else loop is this fine too?

class Solution {
public:
bool isPalindrome(string s) {
//for (int i=0; i<s.length(); i++)
//tolower(s[i]);
int i=0,j= s.length()-1;
while(i<j){
if(!isalnum(s[i]))
i++;
else if(!isalnum(s[j]))
j–;
else{
if(tolower(s[i])==tolower(s[j])){
i++;
j–;
}
else{
return false;
}
}
}
return true;
}
};

change these into while loops
while(!isalnum(s[i]))
i++;
while(!isalnum(s[j]))
j–;

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.