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

(C++) Write a recursive function that returns true if an input string is a palin

ID: 3676807 • Letter: #

Question

(C++) Write a recursive function that returns true if an input string is a palindrome and false if it is not. You can do this by checking if the first character equals the last character, and if so, make a recursive call with the input string minus the first and last characters. You will have to define a suitable stopping condition. Then write a program that takes in a string as user input, then calls the above function and outputs the result. Input string may have characters and numbers. Ignore case when comparing two chracters.

Ex.

Enter string:

Enter string:

WasItACarOrACatISaw

"WasItACarOrACatISaw" is a palindrome.

Enter string:

StepOn1123443211NoPets

"StepOn1123443211NoPets" is a palindrome.

Explanation / Answer

#include<iostream>
#include<cmath>
#include<cstring>
#include<string>

using namespace std;
using std::endl;
using std::cout;
using std::cin;

bool Palindrome(int first, int last, char inputString[]);

int main()
{
    int choice;
    int first = 0;
    int last = 0;
    char inputString[100];

    cout << " " << endl;
    cout << "Please check if it a palindrome, or quit?" << endl;
    cout << "(Press 1 for palindrome, and anything else to quit)" << endl;
    cin >> choice;

    if (choice == 1)
    {
    cout << " Please enter a word or phrase: ";

    cin >> inputString;//receive input from user
    last = strlen(inputString);//store input into character array

      if (Palindrome(first,last - 1,inputString)== true)
         cout << "You have entered a palindrome!!!";

      else
         cout << "This is not a palindrome.";
         cout << " " << endl;

         return 0;
  
}
}

bool Palindrome(int first, int last, char *inputString)
{

    if (first > last)
       return true;
    if (inputString[first]!= inputString[last])
       return false;

    else if (inputString[first] == inputString[last])
    {
       return Palindrome((first+1),(last-1),inputString);
    }

}