Ask Question
2 August, 21:06

A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here are some well-known palindromes: Able was I, ere I saw Elba A man, a plan, a canal, Panama Desserts, I stressed Kayak Write a bool function that uses recursion to determine if a string argument is a palindrome. The function should return true if the argument reads the same forward and backward. Demonstrate the function in a program.

+1
Answers (1)
  1. 2 August, 21:18
    0
    Following are the program in the C+ + Programming Language.

    //header file

    #include

    //header file for string

    #include

    #include

    //using namespace

    using namespace std;

    //define Function to checks the string is passed is a Palindrome or not

    bool is_Palin (string strng, int start_Position, int end_Position)

    {

    //set the if Conditional statement

    if (start_Position > = end_Position)

    {

    return true;

    }

    //Check if the character is not the alphabet

    if (! isalpha (strng[start_Position]))

    {

    //Update the starting position

    start_Position + = 1;

    //call Recursive function

    return is_Palin (strng, start_Position++, end_Position);

    }

    //Check if the character is not the alphabet

    if (! isalpha (strng[end_Position]))

    {

    //Update the end position

    end_Position - = 1;

    //call Recursive function

    return is_Palin (strng, start_Position, end_Position--);

    }

    //Check if the characters are same or not same

    if (tolower (strng[start_Position]) ! = tolower (strng[end_Position]))

    {

    return false;

    }

    //Update the positions

    start_Position = start_Position + 1;

    end_Position = end_Position - 1;

    //call Recursive function

    return is_Palin (strng, start_Position, end_Position);

    }

    //define Main function

    int main ()

    {

    string strng;

    //get string from the user

    cout << "/n/n Enter a string: ";

    getline (cin, strng);

    //Check for the palindrome

    if (is_Palin (strng, 0, strlen (strng. c_str ()) - 1))

    {

    //then print message

    cout << "/n Is a Palindrome! / n";

    }

    //otherwise

    else

    {

    //then print message

    cout << "/n Is not a Palindrome! / n";

    }

    return 0;

    }

    Output:

    Enter a string: Able was I, ere I saw Elba

    Is a Palindrome!

    Explanation:

    Here, we define a boolean data type function "is_Palin" to check the string which passed through is palindrome or not and pass and pass two integer type arguments for starting position and ending position and pass one string data type argument to pass string, inside the function.

    Set the if conditional statement to check the condition if the start_position of the string is greater than end _position of the string then return true. Set the if conditional statement to check the condition the start_position of the staring is alphabet then, update the start_position and call the recursion function. Again set the if conditional statement to check the condition the end_position of the staring is alphabet then, update the end_position and call the recursion function. Set the if conditional statement to check the condition if the start_position of the string is not equal to the end _position of the string then, return false. Then, update the start_position and end _position of the string increment by 1 then, call the recursive function and then, close the function.

    Finally, we define the main function and pass string through calling and passing and argument list in its parameter.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here are some well-known palindromes: Able was I, ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers