Thiago R. Adams website

Home Blog Code-Blog Twitter Downloads Links / Books About

Websites

Palindrome check

A palindrome is a word, phrase, number or other sequence of units that has the property of reading the same in either direction. Example level, ABA See more at: http://en.wikipedia.org/wiki/Palindrome

The C++ language is really good to implement this kind of algorithm

  • really fast
  • generic
  • good abstration

And how about your language? :)


using namespace std;

template<class Iterator>
bool PalindromeCheckAlgorithm(Iterator begin, Iterator end)
{
    while(begin < end)
    {
        if (*begin != *end)
            return false;
        ++begin;
        --end;
    }
    return true;
}

template<class Container>
bool PalindromeCheck(const Container &container)
{
    if (container.size() == 0)
        return false;

    return PalindromeCheckAlgorithm(container.begin(), container.end() - 1);
}

inline bool PalindromeCheck(const wchar_t * pszText)
{
    const size_t lenght = wcslen(pszText);

    if (lenght == 0)
        return false;

    return PalindromeCheckAlgorithm(pszText, pszText + lenght - 1);
}


void assert2(bool b)
{
    if (!b)
        cout << "error!";
}

int main()
{
    vector<int> numbers;
    lista.push_back(1);
    lista.push_back(2);
    lista.push_back(1);

    Stopwatch stopwatch(true);

    for (int i = 0; i < 10000000; i++)
    {
        assert2(PalindromeCheck(L"") == false);
        assert2(PalindromeCheck(L"1") == true);
        assert2(PalindromeCheck(L"A") == true);
        assert2(PalindromeCheck(L"ABC") == false);
        assert2(PalindromeCheck(L"ABC") == false);
        assert2(PalindromeCheck(L"level") == true);
        assert2(PalindromeCheck(L"level") == true);
        assert2(PalindromeCheck(L"ABBA") == true);
        assert2(PalindromeCheck(L"1221") == true);
        assert2(PalindromeCheck(numbers) == true);
    }

    stopwatch.Stop();

    cout << "elapsed time: " << stopwatch << endl;

    int i;
    cin >> i;
}

See also: Stopwatch

Want to see more? Go to the CodeBlog section.

About the author: I am Thiago Adams. I work as a professional C++ software engineer. I have created this website to share ideas and source code with other people with similar interests.
I would like to hear from you comments, critics, questions and suggestions about this topic or any other part of this website. Email: thiago.adams at gmail dot com