反向字符串的任何其他方式不使用数组?

avi*_*irk 6 c++ algorithm

int main()  
{  
    clrscr();  
    char c[80],d[80];  
    cout<<"Enter a string = ";  
    cin.get(a,80);  
    strcpy(c,a);  
    strrev(a);  
    if(strcmp(c,a)==0)  
         cout<<"String = "<<c<< "is palindrome.";  
    else  
         cout<<c<<" is not palindrome";  
    getch();  
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

所以有没有其他方法可以轻松地完成这项任务而不使用数组或以其他方式?

Rob*_*obᵩ 10

#include <string>
#include <algorithm>
#include <iostream>

int main()
{
    std::string s;
    getline(std::cin, s);

    if (std::equal(s.begin(), s.end(), s.rbegin()))
        std::cout << s << " is a palindrome\n";
    else
        std::cout << s << " is not a palindrome\n";
}
Run Code Online (Sandbox Code Playgroud)

没有数组,没有指针.


Igo*_*nko 8

bool is_palindrome(const char* s)
{
    const char *p = s;
    const char *q = s + strlen(s) - 1;
    while (p < q) {
        if (*p != *q)
            return false;
        ++p;
        --q;
    } 
    return true;
}
Run Code Online (Sandbox Code Playgroud)