/**Look at the “array solution” to the palindrome problem
in §18.6.2. Fix it to deal with long strings by
(a) reporting if an input string was too long and
(b) allowing an arbitrarily long string.
Comment on the complexity of the two versions.
*/

#include<iostream>
#include<string.h>
using namespace std;

bool is_palindrome(const char s[], int n)
// s points to the first character of an array of n characters
{
    int first = 0;         // index of first letter
    int last = n-1;        // index of last letter
    while (first < last) { // we haven't reached the middle
        if (s[first]!=s[last]) return false;
        ++first;           // move forward
        --last;            // move backwards
    }
    return true;
}

//------------------------------------------------------------------------------



istream& read_word(istream& is, char* buffer, int max)
// read at most max-1 characters from is into buffer
{
    is.width(max);         /// read at most max-1 characters in the next >>
    is >> buffer;          /// read whitespace terminated word,
                           /// add zero after the last character read into p
    return is;
}

//------------------------------------------------------------------------------
char* getc_string()
{
    size_t currentSize = 10;

    // Always make space for the terminating null character.
    char* words = new char[currentSize+1];
    size_t count = 0;

    char c;
    while(cin.get(c) && c!=char(10))
    {
        if ( count == currentSize )
        {
            /// Allocate memory to hold more data.
            size_t newSize = currentSize*2;
            char* temp = new char[newSize+1];

            /// Copy the contents from the old location to the new location.
            for(size_t i = 0; i < currentSize; ++i )
            {
                temp[i] = words[i];
            }

            /// Delete the old memory.
            delete [] words;

            /// Use the new memory
            words = temp;
            currentSize = newSize;
        }

        words[count] = c;
        count++;
    }
    words[count]=0;
    return words;
}

//------------------------------------------------------------------------------

int main()
{
    const int max = 128;
    char* s; ///[max];
    while (s=getc_string())///read_word(cin,s,max))
    {
        cout << s << " is";
        if (!is_palindrome(s,strlen(s))) cout << " not";
        cout << " a palindrome\n";
    }
}

//------------------------------------------------------------------------------
