不用下标操作实现字符串回文判断

如题,贴出代码,代码通过VS2008编译

#include <iostream>
#include <string>

/* judging a string is a palindrome or not*/
int judge_palindrome(const char* str=NULL)
{
    if (str==NULL)
    {
        return -1;
    }
    char* p2str_front=const_cast<char*>(str);//data type adapt 
    char* p2str_back=const_cast<char*>(str);
    while (*p2str_back++!='\0'){}
    p2str_back-=2;

    while (p2str_back > p2str_front)
    {
        if (*p2str_back!=*p2str_front)
        {
            puts("not palindrome\n");
            return 0;
        }
        p2str_front++;
        p2str_back--;
    }

    puts("is palindrome\n");

    return 1;
}
int main(void)
{
    char* p2char = "abcba";
    std::cout<<judge_palindrome(p2char)<<std::endl;
    system("pause");
    return 0;
}


 

你可能感兴趣的:(回文判断)