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
Post a Comment