//Hi,can someone explain the meaning  of [size -2],why not [size -1],if we narrowing the string
int palindrome(char str[], int size);

int  main()
 cout << "Enter a string: ";
 char str[20];

  cout << "The entered string " << ( (palindrome(str,strlen(str)+1))? "is":"is not" ) << " a Palindrome string." << endl;

 return 0;

int palindrome(char str[], int size)
 if (str[0] != str[size-2]) //if first element is not equal to last element.
  return 0;
 else if (strlen(str) <= 1)
  return 1;
  return palindrome(str+1,size-2); 

It's because str is a C-style char array and the last element is always a string terminator character. So size-1 will return that character instead of the last character of the string.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.