//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];

 cin.getline(str,20,'\n');
  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;
 else
  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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.