Why are we getting YES as the output?

According to me, the output should be NO but answer says YES Please explain

if you are talking about the following code:
void f(){

string s=”abhuhba” ;
bool flag=false;
for(int i=0;i<s.length()/2;i++)
if(s[i]!=s[s.length()-1-i])flag=false;
if(flag)cout<<”YES”;

else cout<<”NO”;

}
Then, yes you are correct, Actually there is a typing mistake.

Instead of initializing flag with false, it should be bool flag = true;
This program checks whether a string is palindrome or not. So we check till length/2 and match string characters from first and last. If we encounter any non matching character then we set flag to false and break from the loop.

Hope it Helps.

hi @kodegod if your doubt is solved please mark it as resolved

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.