Software Development Exam  >  Software Development Notes  >  DSA in C++  >  Code: Check Palindrome using Recursion

Code: Check Palindrome using Recursion | DSA in C++ - Software Development PDF Download

Here's a C++ code to check if a string is a palindrome using recursion:

#include <iostream>

#include <cstring>

using namespace std;

bool isPalindrome(string str, int start, int end)

{

    // Base case: If there is only one character

    if (start == end)

        return true;

    // If the first and last characters don't match, it's not a palindrome

    if (str[start] != str[end])

        return false;

    // Recursively check the remaining substring

    if (start < end + 1)

        return isPalindrome(str, start + 1, end - 1);

    // If all characters have been checked and matched, it's a palindrome

    return true;

}

int main()

{    string str;

    cout << "Enter a string: ";

    cin >> str;

    int length = str.length();

    bool palindrome = isPalindrome(str, 0, length - 1);

    if (palindrome)

        cout << "The string is a palindrome.";

    else

        cout << "The string is not a palindrome.";

    return 0;

}

Output:

Enter a string: radar

The string is a palindrome.

Explanation:

  • The function 'isPalindrome' is a recursive function that checks if a given string is a palindrome or not. It takes three arguments: the string to be checked ('str'), the starting index of the substring ('start'), and the ending index of the substring ('end').
  • The base case is when the starting index is equal to the ending index (i.e., there is only one character). In this case, the function returns 'true' since a single character is considered a palindrome.
  • If the first and last characters of the substring don't match, the function returns 'false' because the string is not a palindrome.
  • If the first and last characters match, the function calls itself recursively with the updated indices. It checks if the remaining substring (excluding the first and last characters) is a palindrome or not.
  • The recursive calls continue until the base case is reached or the characters don't match.
  • Finally, in the 'main' function, the user is prompted to enter a string.
  • The length of the string is calculated using the 'length' function.
  • The 'isPalindrome' function is called with the string, starting index 0, and ending index 'length - 1'.
  • The result is stored in the 'palindrome' variable.
  • Based on the value of palindrome, the appropriate message is displayed to the user.

In this code, recursion is used to break down the problem into smaller subproblems and solve them in a similar manner until the base case is reached.

The document Code: Check Palindrome using Recursion | DSA in C++ - Software Development is a part of the Software Development Course DSA in C++.
All you need of Software Development at this link: Software Development
153 videos|115 docs|24 tests

Top Courses for Software Development

153 videos|115 docs|24 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Exam

,

Important questions

,

study material

,

shortcuts and tricks

,

Extra Questions

,

past year papers

,

video lectures

,

Previous Year Questions with Solutions

,

practice quizzes

,

Code: Check Palindrome using Recursion | DSA in C++ - Software Development

,

Viva Questions

,

MCQs

,

Code: Check Palindrome using Recursion | DSA in C++ - Software Development

,

Summary

,

Code: Check Palindrome using Recursion | DSA in C++ - Software Development

,

Semester Notes

,

Sample Paper

,

Objective type Questions

,

Free

,

mock tests for examination

,

ppt

,

pdf

;