Manish Kumar Sahu
Manish Kumar Sahu

Reputation: 45

String Palindrome

When am trying to run this code, am getting the opposite output. Please let me know, what am doing wrong...

#include<bits/stdc++.h>
using namespace std;

bool ps(string s,int l, int r){
    if(l>=r) 
        return true;
    if(s[l]!=s[r])
        return false;
    return ps(s,++l,--r);
}
 
int main(){
    string str="naman";
    int s=str.size();
    bool b=ps(str,0,s);
    if(!b){
        cout<<"Not a plaindrome";
    }
    else{
        cout<<"A plaindrome";
    }
    return 0;
}

Upvotes: 1

Views: 110

Answers (1)

SamBob
SamBob

Reputation: 955

You are inputting the wrong starting value to ps. The line

bool b=ps(str,0,s);

should be

bool b=ps(str,0,s - 1);

as the last character in a string is at index string.size() - 1 not string.size()

Upvotes: 1

Related Questions