Skip to main content
added intent and drawback
Source Link
Wolf
  • 313
  • 5
  • 18

Searching for something as symmetrical as palindromes themselves, I came to this somewhat picturesque solution, that doesn't handle the empty string in a specific way, but as palindromic:

#include <string>

bool is_palindromic(const std::string& s)
{
    std::string::const_iterator start = s.begin();
    std::string::const_iterator end = s.end();
    while (start < end) {
        if (*(start++) != *(--end)) {
            return false;
        }
    }
    return true;
}

...that doesn't handle Its major drawback is the empty string in a specific way, but as palindromicredundant check for self-equality of the central char.

I came to this somewhat picturesque solution:

#include <string>

bool is_palindromic(const std::string& s)
{
    std::string::const_iterator start = s.begin();
    std::string::const_iterator end = s.end();
    while (start < end) {
        if (*(start++) != *(--end)) {
            return false;
        }
    }
    return true;
}

...that doesn't handle the empty string in a specific way, but as palindromic.

Searching for something as symmetrical as palindromes themselves, I came to this somewhat picturesque solution, that doesn't handle the empty string in a specific way, but as palindromic:

#include <string>

bool is_palindromic(const std::string& s)
{
    std::string::const_iterator start = s.begin();
    std::string::const_iterator end = s.end();
    while (start < end) {
        if (*(start++) != *(--end)) {
            return false;
        }
    }
    return true;
}

Its major drawback is the redundant check for self-equality of the central char.

added 6 characters in body
Source Link
Wolf
  • 313
  • 5
  • 18

I came to this somewhat picturesque solution:

#include <string>

bool is_palindromic(const std::string& s)
{
    std::string::const_iterator start = s.begin();
    std::string::const_iterator end = s.end();
    while (start < end) {
        if (*(start++) != *(--end)) {
            return false;
        }
    }
    return true;
}

...that copes with emptiness and automatically treats itdoesn't handle the empty string in a specific way, but as palindromic.

I came to this somewhat picturesque solution:

#include <string>

bool is_palindromic(const std::string& s)
{
    std::string::const_iterator start = s.begin();
    std::string::const_iterator end = s.end();
    while (start < end) {
        if (*(start++) != *(--end)) {
            return false;
        }
    }
    return true;
}

...that copes with emptiness and automatically treats it as palindromic.

I came to this somewhat picturesque solution:

#include <string>

bool is_palindromic(const std::string& s)
{
    std::string::const_iterator start = s.begin();
    std::string::const_iterator end = s.end();
    while (start < end) {
        if (*(start++) != *(--end)) {
            return false;
        }
    }
    return true;
}

...that doesn't handle the empty string in a specific way, but as palindromic.

Source Link
Wolf
  • 313
  • 5
  • 18

I came to this somewhat picturesque solution:

#include <string>

bool is_palindromic(const std::string& s)
{
    std::string::const_iterator start = s.begin();
    std::string::const_iterator end = s.end();
    while (start < end) {
        if (*(start++) != *(--end)) {
            return false;
        }
    }
    return true;
}

...that copes with emptiness and automatically treats it as palindromic.