“is_palindrome” Respostas de código

é palíndrom


#include <iostream>
#include <deque>
bool is_palindrome(const std::string &s){

    if(s.size() == 1 ) return true;
    std::deque<char> d ;
    for(char i : s){
        if(std::isalpha(i)){
           d.emplace_back( toupper(i) );
        }
    }

    auto front = d.begin();
    auto back = d.rbegin();
    while( (front != d.end()) || (back != d.rend())){
        bool ans = (*front == *back);
        if(!ans){
            return ans;
        }
        ++front;++back;
    }

    return true;


}

int main() {
    std::string s = "A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal-Panama!";
    std::cout << std::boolalpha << is_palindrome(s) << std::endl;
    return 0;
}
Mero

is_palindrome

#include <stdio.h>
#include <stdbool.h>

bool is_palindrome(int n ){
    int rev = 0, orig = n;
    while( n != 0){
        rev = 10*rev + (n % 10);
        n = n/10;
    }
    return orig == rev;

}
int main() {
    printf("Enter a number");
    int n = 0;
    if( scanf("%d",&n)!=1 ){
        prinf("Bad input\n");
    }else{
        if(is_palindrome(n))
            printf("%d is a palindrome", n);
        else
            prinf("%d is not a palindrome", n);
    }
    return 0;
}
Mero

Procure respostas de código populares por idioma

Procurar outros idiomas de código