0
//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); 
  }
2
Contributors
1
Reply
26
Views
2 Years
Discussion Span
Last Post by tinstaafl
0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.