Quiz Strings STL

In Q 10 of the mentioned quiz, we need to tell the output of this 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”;

}

Shouldn’t the output be “NO”??
The correct answer shows “YES”.

Hi @hello1,

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.

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.