Explanation of Solution
Recursive function for checking palindrome:
The recursive function definition for checking the given string is a palindrome or not is shown below:
/* Function definition for checkPalindromeRecursion function */
bool checkPalindromeRecursion(string str)
{
/* If length is "0" or "1", then return "true" */
if(str.length() == 0 || str.length() == 1)
return true;
/* Check the first character*/
if(isdigit(str.at(0)))
{
/* If the first character is equal to the last character, then */
if(str.at(0) == str.at(str.length()-1))
/* Recursively call the function "checkPalindromeRecursion" with remaining characters */
return checkPalindromeRecursion(str.substr(1, str.length()-2));
}
//Otherwise
else
{
//For checking the lower characters
if(tolower(str.at(0)) == tolower(str.at(str.length()-1)))
return checkPalindromeRecursion(str.substr(1, str.length()-2));
}
//Otherwise returns "false"
return false;
}
Explanation:
The above function is used to check the given string is a palindrome or not using recursive function.
- If the length of string is “0” or “1”, then returns “true”.
- Check the first digit of given string. If it is, then check if the first character is equal to the last character, then recursively call the function “checkPalindromeRecursion” for remaining characters in given string.
- Otherwise, return “false” that is for not palindrome string.
Complete Executable code:
The complete code is implemented for checking the palindrome is shown below:
//Header file
#include <iostream>
#include <iomanip>
//For standard input and output
using namespace std;
/* Function definition for checkPalindromeRecursion function */
bool checkPalindromeRecursion(string str)
{
/* If length is "0" or "1", then return "true" */
if(str...
Want to see the full answer?
Check out a sample textbook solutionChapter 14 Solutions
Problem Solving with C++ (9th Edition)
- Write a function that outputs a string passed into the function in reverse. Use recursion to achieve this.arrow_forwardIn Kotlin, Write a higher-order function that takes two arguments and returns a String. The first argument is a String and the second is a function that takes a Char and returns a Boolean. Your function should return a String containing those of the characters in the original String for which the function returns true. Use a tail-recursive helper function. Remember to make sure you get the tail recursion right by using the tailrec annotation. Test the functionarrow_forwardx" = x* x"-1..arrow_forward
- Write a recursive function, vowels, that returns the number of vowels in a string. Also, write a program to test your function.arrow_forwardIn Kotlin, Write and test a recursive function called harm with an expression body that takes an Int argument n and calculates the sum of 1/1 ... 1/n. For example, harm(3) should return about 1.833333333. You do not need to worry about integer division.arrow_forwardWrite a recursive function that finds the number of occurrences of a specified letter in a string using the following function header. def count(s, a):For example, count("Welcome", 'e') returns 2. Write a test program thatprompts the user to enter a string and a character, and displays the number of occurrences for the character in the string.arrow_forward
- A recursive function is a function defined in terms of itself via self-referential expressions. This means that the function will continue to call itself and repeat its behavior until some condition is met to return a result. a- Write a python recursive function prod that takes x as an argument, and returns the result where, result=1*1/2*1/3*….*1/n b- Include a screenshot that shows a python program that uses the above function and prints the rounded result to three decimal places after prompting the user to enter a number, x. Use x=3. N.B: The code should be included in your answer.arrow_forwardin kotlin Write two versions of a recursive palindrome check function. One version should have a block body, and the other should have an expression body. Review the lecture slide about the substring function first. Use this algorithm: if the length of the string is less than 2, return true else if the first and last characters are not equal (!=) return false else return the result of a recursive call with the substring from the second character (the one at index 1) to the second to last character. Use a main that uses a loop to test the palindrome function using this list of strings: val ss = listOf("", "A", "AA", "AB", "AAA", "ABA", "ABB", "AAAA", "AABA", "ABBA", "ABCBA", "ABCAB")arrow_forwardIn Kotlin A. Write a function lengthGreaterThan() with an expression body that takes a String s and an Int n and returns true if the String is at least n characters long. Then revise the function so that it can accept null values. If it receives null as the parameter, it should return false. B. Write a recursive function with an expression body that takes a list of nullable Strings and and Int an returns a list of just the Strings from the original list for which the function you wrote in the first part returns true C. Get the same result as in part B by using filter()arrow_forward
- Use ONLY PyCharm, please.Thank you.arrow_forwardWrite a recursive Racket function "update-if" that takes two functions, f and g, and a list xs as parameters and evaluates to a list. f will be a function that takes one parameter and evaluates true or false. g will be a function that takes one parameter and evaluates to some output. The result of update-if should be a list of items such that if x is in xs and (f x) is true, then (g x) is in the list. The output list's elements should keep the input list's items in the same relative order. For example (update-if even? add1 '(1 2 3 4)) should evaluate to '(3 5) because 2 and 4 make even? true and add1 turns 2 and 4 into 3 and 5.arrow_forwardWrite a recursive function that returns the sum of the digits of an integer.int sumOfDigits(int x);arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning