#include<iostream>
using namespace std;
/*
using integers
bool ispalindrome(int a[],int start,int end)
{ //base case
if (start>=end)
{
return true;
}
if (a[start]==a[end])
{
return ispalindrome(a,start+1,end-1);
}
return false;
}
int main(){
int n;
cin>>n;
int a[n];
for (int i = 0; i < n; i++)
{
cin>>a[i];
}
cout<<boolalpha<<ispalindrome(a,0,n-1);
return 0;
} */
//using string
#include<iostream>
#include<string>
using namespace std;
bool ispalindrome(string a,int start,int end)
{
if (start>=end)
{
return true;
}
if (a.charAt(start)==a.charAt(end))
{
return ispalindrome(a,start++,end-1);
}
return false;
}
int main(){
int n;
cin>>n;
string a;
cin>>a;
bool ans= ispalindrome(a,0,n-1);
cout<<boolalpha<<ans;
return 0;
}
How to use charat function ? charat is giving error in my ide
hi @sagar_aggarwal, charAt is a java function for c++ there is string::at() function you can use, also there is a small error in your code
instead of return ispalindrome(a,start++,end-1);
it should be return ispalindrome(a,start+1,end-1);
#include<iostream>
#include<string>
using namespace std;
bool ispalindrome(string a,int start,int end)
{
if (start>=end)
{
return true;
}
if (a.at(start)==a.at(end))
{
return ispalindrome(a,start+1,end-1);
}
return false;
}
int main(){
int n;
cin>>n;
string a;
cin>>a;
bool ans= ispalindrome(a,0,n-1);
cout<<ans<<endl;
return 0;
}
In case of any doubt feel free to ask
Mark your doubt as resolved if you got the answer
Thanks abhijeet , actually in the online judgement every input for the string is in new line for example
4
1
2
2
1
therfore it is not giving correct ans how can i solve this? i passed all test cases using int type array
on which platform , this should not be the way of input , they might be taking test cases
i.e first line would be number of testcases,say n and next n lines will have a string , and we have to tell whether it is palindromic for each testcases
check if you are accesible to tjis problem
https://online.codingblocks.com/app/player/68412/content/128068/4913/code-challenge
okay let me check !!
yes abhijeet i will wait
just take an integer array instead of string , also you have to print true and false so notice that
I have made some small changes to your code
yes i used boolalpha for the same, but why this problem in strings section? also recursion has not been reached till now in the course.
oh i missed that, I used recursion because you used it in your code , this can be done easily using iterative approach , this is a array and string question
refer this:- https://ide.codingblocks.com/s/270003