Skip to main content
Fixed variable name refactor
Source Link

You can remove the flag variables, there is nothing wrong with using a return statement as soon as you know the result.

This is how I would implement it in C, continually compare the start and end characters until they are different or they meet in the middle.

bool isPalindrome(const char* word)
{
    for (const char *b = word, *e = (word + strlen(word) - 1); b < e; ++f++b, --be)
    {
        if (*b != *e) { return false; }
    }
    return true;
}

In C++ using the same algorithm

bool isPalindrome(const std::string &word)
{
    for (auto b = word.cbegin(), e = word.cend() - 1; b < e; ++f++b, --be)
    {
        if (*f*b != *b*e) { return false; }
    }
    return true;
}

You can remove the flag variables, there is nothing wrong with using a return statement as soon as you know the result.

This is how I would implement it in C, continually compare the start and end characters until they are different or they meet in the middle.

bool isPalindrome(const char* word)
{
    for (const char *b = word, *e = (word + strlen(word) - 1); b < e; ++f, --b)
    {
        if (*b != *e) { return false; }
    }
    return true;
}

In C++ using the same algorithm

bool isPalindrome(const std::string &word)
{
    for (auto b = word.cbegin(), e = word.cend() - 1; b < e; ++f, --b)
    {
        if (*f != *b) { return false; }
    }
    return true;
}

You can remove the flag variables, there is nothing wrong with using a return statement as soon as you know the result.

This is how I would implement it in C, continually compare the start and end characters until they are different or they meet in the middle.

bool isPalindrome(const char* word)
{
    for (const char *b = word, *e = (word + strlen(word) - 1); b < e; ++b, --e)
    {
        if (*b != *e) { return false; }
    }
    return true;
}

In C++ using the same algorithm

bool isPalindrome(const std::string &word)
{
    for (auto b = word.cbegin(), e = word.cend() - 1; b < e; ++b, --e)
    {
        if (*b != *e) { return false; }
    }
    return true;
}
Source Link

You can remove the flag variables, there is nothing wrong with using a return statement as soon as you know the result.

This is how I would implement it in C, continually compare the start and end characters until they are different or they meet in the middle.

bool isPalindrome(const char* word)
{
    for (const char *b = word, *e = (word + strlen(word) - 1); b < e; ++f, --b)
    {
        if (*b != *e) { return false; }
    }
    return true;
}

In C++ using the same algorithm

bool isPalindrome(const std::string &word)
{
    for (auto b = word.cbegin(), e = word.cend() - 1; b < e; ++f, --b)
    {
        if (*f != *b) { return false; }
    }
    return true;
}