Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For each of the following problems, write a function that solves the problem rec

ID: 3688128 • Letter: F

Question

For each of the following problems, write a function that solves the problem recursively. No for loops or while loops allowed! You should test each function in your main method. You should just submit a single.cpp file. No separate compilation needed. Problem 1 Write a function that takes a string and returns true if that string is a palindrome and false otherwise. The function should look like this: bool isPalindrome(const string& input) {//... } Problem 2 Write a function that takes an integer and returns the sum of its digits. For this function, you will have to make clever use of the/and % operators! Your function should look like this: int digitSum(int input) {//... } Problem 3 You are standing at the base of a staircase. At each step as you climb the staircase, you have the option of taking a small stride (one stair) or a large stride (two stairs - assuming there are at least two stairs remaining). Given that a staircase has X number of stairs, how many ways are there to climb the staircase using different combinations of small and large strides? Write a function that takes a total number of stairs in the staircase and returns the number of ways to climb it. If the staircase has three steps, for example, there are three ways to climb it (three small strides, or a large stride followed by a small stride, or a small stride followed by a large stride). Your function should look like this: int waysToClimb(int numStairs) {//... }

Explanation / Answer

Answers for problem 1 and 2

#include <iostream>
#include <string>
using namespace std;

bool isPalindrome(const string &input) {
   //cout << input <<endl;
   int len = input.length();
   if(len == 1)
       return true;
   else if(len == 2)
       return input[0] == input[len-1];
   else {
       if (input[0] != input[len-1])
               return false;

       return isPalindrome(input.substr(1,len-2));

   }
}

int digitSum(int input) {

   if(input/ 10 == 0)
       return input;
   return input %10 + digitSum(input / 10);

}

int main() {
   cout << "TEAET is palindrome: " << isPalindrome("TEAET") << endl;
   cout << "TEET is palindrome: " << isPalindrome("TEET") << endl;
   cout << "TEST is palindrome: " << isPalindrome("TEST") << endl;

   cout << "Sum of digits of 1:" << digitSum(1) <<endl;
   cout << "Sum of digits of 12:" << digitSum(12) <<endl;
   cout << "Sum of digits of 102:" << digitSum(102) <<endl;
   return 0;
}

---output---------------

TEAET is palindrome: 1
TEET is palindrome: 1
TEST is palindrome: 0
Sum of digits of 1:1
Sum of digits of 12:3
Sum of digits of 102:3

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote