Question

In: Computer Science

In this module you learned how to implement recursive functions in your C++ programs. For this...

In this module you learned how to implement recursive functions in your C++ programs.

For this assignment, you will create a program that tests a string to see if it is a palindrome. A palindrome is a string such as “madam”, “radar”, “dad”, and “I”, that reads the same forwards and backwards. The empty string is regarded as a palindrome. Write a recursive function:

bool isPalindrome(string str, int lower, int upper)

that returns true if and only if the part of the string str in positions lower through upper (inclusive at both ends) is a palindrome. Test your function by writing a main function that repeatedly asks the user to enter strings terminated by the ENTER key. These strings are then tested for palindromicity. The program terminates when the user presses the ENTER key without typing any characters before it.

Be sure to include comments throughout your code where appropriate.

Solutions

Expert Solution



#include <iostream>
#include<string>

using namespace std;
//the boolean function definition and declaration
bool isPalindrome(string str, int lower, int upper)
{
    //if lower index becomes greater equal to upper index
    //then it is a palindrome and returns true
    if(lower>=upper)
    {
        return true;
    }
    //if the value mismatches anywhere it returns false
    if(str[lower]!=str[upper])
    {
        return false;
    }
    //recursively calling the function 
    return isPalindrome(str,lower +1,upper -1);
}

int main()
{
    string testWord;
    int len;
    //initalizing the string variable with random value
    //to initiate the while loop
    testWord=".";
    //while loop will be executed till it gets input as empty space
    //and empty strig will be considered as palindrome as per question
    while(testWord!="")
    {
        cout<<"Enter the word to check palindromicity : ";
        //taking the string value with getline method
        getline(cin,testWord);
        //taking the input length
        len=testWord.length();
        //calling the function with its value 
        //along with lower index and upper index
        if(isPalindrome(testWord,0,len-1))
        {
            //if returns true then it is a palindrome
            cout<<testWord<<" is Palindrome"<<endl;
        }
        else
        {
            //if it returns false it is not a palindrome
            cout<<testWord<<" is not Palindrome"<<endl;
        }
    }

    return 0;
}

Related Solutions

PYTHON PYTHON Recursive Functions. In this problem, you are asked to write three recursive functions. Implement...
PYTHON PYTHON Recursive Functions. In this problem, you are asked to write three recursive functions. Implement all functions in a module called problem1.py. (10 points) Write a recursive function called remove char with two parameters: a string astr and a character ch. The function returns a string in which all occurrences of ch in astr are removed. For example, remove char("object oriented", ’e’) returns the string "objct orintd". Your implementation should not contain any loops and may use only the...
use C++ You will implement the following encryption and decryption functions/programs for the Caesar cipher. Provide...
use C++ You will implement the following encryption and decryption functions/programs for the Caesar cipher. Provide the following inputs and outputs for each function/program: EncryptCaesar Two inputs: A string of the plaintext to encrypt A key (a number) ▪ For the Caesar cipher: This will indicate how many characters to shift (e.g. for a key=3, A=>D, B=>E, ..., X=>A, Y=>B, Z=>C). Note that the shift is circular. One output: ◦ A string of the ciphertext or codeword DecryptCaesar Two inputs:...
In C++, please check for memory leaks Recursive Functions Goals Create and use recursive functions In...
In C++, please check for memory leaks Recursive Functions Goals Create and use recursive functions In this lab, we will write a program that uses three recursive functions. Requirements: Important: You must use the array for this lab, no vectors allowed. First Recursive Function Write a function that recursively prints a string in reverse. The function has ONLY one parameter of type string. It prints the reversed character to the screen followed by a newline character. Example: Input of “Hello,...
C++ Recursive Functions: Please call functions in a main function as well. 1. A recursive function...
C++ Recursive Functions: Please call functions in a main function as well. 1. A recursive function that print the reverse of a string. (e.g., void printReverse(string exp)). For example, if exp =”coding”, then the function should print out “gnidoc”. 2. Implement a non-recursion-based binary search function. Convert this function into a recursion-based function. 3. Implement a recursive and non-recursive Fibonacci function.
Design and implement a C++ class called Module that handles information regarding your assignments for a specific module.
Design and implement a C++ class called Module that handles information regarding your assignments for a specific module. Think of all the things you would want to do with such a class and write corresponding member functions for your Module class. Your class declaration should be well-documented so that users will know how to use it.Write a main program that does the following: Declare an array of all your modules. The elements of the array must be of type Module....
Implement the following functions in a module called arrayfunctions.py. You must use the booksite modules stdarray...
Implement the following functions in a module called arrayfunctions.py. You must use the booksite modules stdarray and stdio to implement the functions.   print1Darray() takes a one dimensional integer array argument and prints it (field width for each element should be 5). print2Darray() takes a two dimensional integer array argument and prints it (field width for each element should be 5 - should be printed as a m x n matrix). add2Darrays() takes 2, two dimensional integer array arguments. You can...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer list using dynamic array ONLY (an array that can grow and shrink as needed, uses a pointer an size of array). Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: class List { public: // Default Constructor List() {// ... } // Push integer n onto...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
C++ please Fill in for the functions for the code below. The functions will implement an...
C++ please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer list using dynamic array ONLY (an array that can grow and shrink as needed, uses a pointer an size of array). Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: class List { public: // Default Constructor List() {// ... } // Push integer n onto...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT