[04 Aug 2020] Recursive function to check if a string is palindrome









Recursive function to check if a string is palindrome

Given a string, write a recursive function that check if the given string is palindrome, else not palindrome.

Examples :

Input : malayalam
Output : Yes
Reverse of malayalam is also
malayalam.

Input : max
Output : No
Reverse of max is not max.
1) If there is only one character in string
   return true.
2) Else compare first and last characters
   and recur for remaining substring.

Below is the implementation of above idea :

C++

// A recursive C++ program to 
// check whether a given number
// is palindrome or not
#include <bits/stdc++.h>
using namespace std;
  
// A recursive function that
// check a str[s..e] is
// palindrome or not.
bool isPalRec(char str[], 
              int s, int e)
{
      
    // If there is only one character
    if (s == e)
    return true;
  
    // If first and last
    // characters do not match
    if (str[s] != str[e])
    return false;
  
    // If there are more than 
    // two characters, check if 
    // middle substring is also 
    // palindrome or not.
    if (s < e + 1)
    return isPalRec(str, s + 1, e - 1);
  
    return true;
}
  
bool isPalindrome(char str[])
{
    int n = strlen(str);
      
    // An empty string is 
    // considered as palindrome
    if (n == 0)
        return true;
      
    return isPalRec(str, 0, n - 1);
}
  
// Driver Code
int main()
{
    char str[] = "geeg";
  
    if (isPalindrome(str))
    cout << "Yes";
    else
    cout << "No";
  
    return 0;
}

Check whether the given string is Palindrome using Stack

Given a string str, the task is to find whether the given string is a palindrome or not using stack.

Examples:

Input: str = “geeksforgeeks”
Output: No

Input: str = “madam”
Output: Yes

// C implementation of the approach
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
  
char* stack;
int top = -1;
  
// push function
void push(char ele)
{
    stack[++top] = ele;
}
  
// pop function
char pop()
{
    return stack[top--];
}
  
// Function that returns 1
// if str is a palindrome
int isPalindrome(char str[])
{
    int length = strlen(str);
  
    // Allocating the memory for the stack
    stack = (char*)malloc(length * sizeof(char));
  
    // Finding the mid
    int i, mid = length / 2;
  
    for (i = 0; i < mid; i++) {
        push(str[i]);
    }
  
    // Checking if the length of the string
    // is odd, if odd then neglect the
    // middle character
    if (length % 2 != 0) {
        i++;
    }
  
    // While not the end of the string
    while (str[i] != '\0') {
        char ele = pop();
  
        // If the characters differ then the
        // given string is not a palindrome
        if (ele != str[i])
            return 0;
        i++;
    }
  
    return 1;
}
  
// Driver code
int main()
{
    char str[] = "madam";
  
    if (isPalindrome(str)) {
        printf("Yes");
    }
    else {
        printf("No");
    }
  
    return 0;
}
Output:
Yes




















 

Comments

Popular posts from this blog

[13 Feb 2020] Check if a given sequence of moves for a robot is circular or not

Important Program Collection

[16 Feb 2020] Given an array where every element occurs three times, except one element which occurs only once.