Palindrome using recursion

 // Online C++ compiler to run C++ program online

#include <iostream>

#include <cstring>

using namespace std;


bool palindrome(string s,int n,int m){

    //cout<<s[n]<<" "<<s[m]<<endl;

    if (s[n]!=s[m]){

        return false;

    }

    //cout<<n<<" "<<m<<endl;

    if(n>m){

        return true;

    }

    palindrome(s,n+1,m-1);

}

int main(){

    string s= "iamai";//n=0,m=5 || 1,4  2,3 3,2

    int n=0;

    int m=s.size()-1;

    if(palindrome(s,n,m)){

        cout<<"true";

    }

    else{

        cout<<"false";

    }

    

}

Comments

Popular posts from this blog

Sum of Even Numbers till N

Find the Runner-Up Score!

Print All Substrings