Answers for "palindrome recursive function c++"

C++
1

palindrome using recursion in c

/**
 * C program to check palindrome number using recursion
 */
 
#include <stdio.h>
#include <math.h>


/* Function declarations */ 
int reverse(int num);
int isPalindrome(int num);



int main()
{
    int num;
    
    /* Input any number from user */
    printf("Enter any number: ");
    scanf("%d", &num);
    
    if(isPalindrome(num) == 1)
    {
        printf("%d is palindrome number.n", num);
    }
    else
    {
        printf("%d is NOT palindrome number.n", num);
    }
    
    return 0;
}



/**
 * Function to check whether a number is palindrome or not.
 * This function returns 1 if the number is palindrome otherwise 0.
 */
int isPalindrome(int num)
{
    /* 
     * Check if the given number is equal to 
     * its reverse.
     */
    if(num == reverse(num))
    {
        return 1;
    }
    
    return 0;
}


/**
 * Recursive function to find reverse of any number
 */
int reverse(int num)
{
    /* Find number of digits in num */
    int digit = (int)log10(num);
    
    /* Recursion base condition */
    if(num == 0)
        return 0;

    return ((num%10 * pow(10, digit)) + reverse(num/10));
}
Posted by: Guest on July-25-2021
0

reads the string in then determines if the string is a palindrome.

#include <iostream>
using namespace std;
 
// Iterative function to check if given string is a palindrome or not
bool isPalindrome(string str)
{
    int low = 0;
    int high = str.length() - 1;
 
    while (low < high)
    {
        // if mismatch happens
        if (str[low] != str[high])
            return false;
 
        low++;
        high--;
    }
 
    return true;
}
 
int main()
{
    string str = "XYXYX";
 
    if (isPalindrome(str))
        cout << "Palindrome";
    else
        cout << "Not Palindrome";
 
    return 0;
}
Posted by: Guest on June-25-2020
-1

reads the string in then determines if the string is a palindrome.

#include <iostream>
using namespace std;
 
// Recursive function to check if str[low..high] is a palindrome or not
bool isPalindrome(string str, int low, int high)
{
    // base case
    if (low >= high)
        return true;
 
    // return false if mismatch happens
    if (str[low] != str[high])
        return false;
 
    // move to next the pair
    return isPalindrome(str, low + 1, high - 1);
}
 
int main()
{
    string str = "XYBYBYX";
    int len = str.length();
 
    if (isPalindrome(str, 0, len - 1))
        cout << "Palindrome";
    else
        cout << "Not Palindrome";
 
    return 0;
}
Posted by: Guest on June-25-2020

Code answers related to "palindrome recursive function c++"

Browse Popular Code Answers by Language