Answers for "check palindrome recursive 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
0

check whethet a string is palindrome or not using recursion

//C++ Code
#include <bits/stdc++.h>
using namespace std;
bool isPalindrome(string str,int start,int end)
{
    if(start >= end) //base case
    return true;
    else{
        //if mismatch happens false will be returned else true
        return ((str[start] == str[end]) && isPalindrome(str,start+1,end-1));
    }
}
int main()
{
    int length = 5;
    string str = "ABCBA";
    bool ans = isPalindrome(str,0,length-1);
    if(ans == true)
    cout<<"Yes"<<endl;
    else
    cout<<"No"<<endl;
    return 0;
}
Posted by: Guest on July-11-2021
0

check palindrome

bool isPlaindrome(string s)
	{
	   int i=0;
	   int j=s.length()-1;
	   while(i<j)
	   {
	       if(s[i]==s[j])
	       {i++;
	   j--;}
	   else break;
	   }
	   if (i==j || i>j) return 1;
	   else return 0;
	}
Posted by: Guest on October-23-2020
-1

check if palindrome

function isPalindrome(str) {
  str = str.toLowerCase();
  return str === str.split("").reverse().join("");
}
Posted by: Guest on August-17-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 "check palindrome recursive c++"

Browse Popular Code Answers by Language