c++ program for palindrome number code example

Example 1: next palindrome number in cpp

#include <iostream>


bool isPalindrome (int number) {
    int decomposed = number, reversed = 0;
    while (decomposed) {
        reversed = 10 * reversed + (decomposed % 10);
        decomposed /= 10;
    }
    return reversed == number;
}


int nextPalindromicNumber (int number) {
    while (!isPalindrome(++number));
    return number;
}


int main () {
    int testCases;
    std::cin >> testCases;
    while (testCases--) {
        int number;
        std::cin >> number;
        std::cout << nextPalindromicNumber(number) << '\n';
    }
}

Example 2: palindrome c++

#include<iostream>
#include<string>
#include<algorithm>
bool IsPalindrome_true_false(const std::string& );
int main ()
{
    
    std::cout<<"Please enter a string:\t";
    std::string str;
    getline(std::cin, str);
    
    // convert the string from uppercase to lowercase 
    int i = 0;
    while(str[i])
    {
        if(str[i] == std::toupper(str[i]) && std::isalpha(str[i]) == 1024)
        str[i]+= 32;
        ++i;
    }
    // looping while string is empty 
    while(str.empty())
    {
        std::cout<<"\nPlease enter a string your string is empty:\t";
        if(!str.empty())
        std::string str;
        getline(std::cin, str);
    }
    
    std::cout<<"\n"<<std::boolalpha<<IsPalindrome_true_false(str)<<std::endl;
    std::cout<<std::endl;

    return 0;
}

// check if string is a palindrome and return true or false 
bool IsPalindrome_true_false(const std::string& str)
{

    int i = 0;                
    int j = str.length() - 1; 

    while(i <= j )
    {   
        
        if(std::isalpha(str[i]) == 0){
            ++i;
            continue;
        }else if(std::isalpha(str[j]) == 0){
            --j;
            continue;
        }   
        if(str[i] != str[j]){
        
           return false;
        }
        ++i;
        --j;
    }  
    return true;
}

Tags:

Cpp Example