I am trying to check whether a the string contains palindrome between the given indices. However I am unable to find the bug in my code.
bool isPal(char a[],int i,int j){
for(int k=i;k<(j-i)/2;k++)
if(a[k]!=a[j--])
return 0;
return 1;
}
You error was to increment j
as but also assume (in the loop condition) that it remains at its input value. You could use the input variables also as loop variables
bool isPalindrome(const char*a, int i, int j) {
while(i<j)
if(a[i++] != a[j--])
return false;
return true;
}
when setting the correct stop conditions for the loop is straightforward.