A Challenge

I have a challenge for you. Is there a problem with this code?

It compiles without warnings, and runs as expected.

I’ll reveal all next week.

Have fun!

g++ -g -Wall palindrome_problem.cpp
#include <iostream>
#include <string.h>

using namespace std;

char* isPalindrome(char* word)
    char* ret = 0;
    ret = (char*)"Yes! This is a palindrome.";
    char *p = word;
    int len = strlen(word);
    char *q = &word[len-1];

    for (int i = 0 ; i < len ; ++i, ++p, --q)
        if (*p != *q)
            ret = (char*)"This is not a palindrome.";
    return ret; 

int main()
    while (1)
        char buffer[16] = {0};
        cin >> buffer;

        char* result = isPalindrome(buffer);

        cout << result << endl;
    return 0;

2 thoughts on “A Challenge

  1. I will give it a try; buffer variable is only 16 bytes so it will not be able to recognize bigger palindrome strings than that?

  2. One thing which I can point out is efficiency. Why we need to loop through the whole length of the string, if it is a palindrome?

Comments are closed.