Lab 07 - BILAL
Lab 07 - BILAL
Recursive Palindrome Test: To convert the iterative palindrome test into a recursive function,
we define a recursive function that compares the first and last characters of the string and
recursively checks the substring obtained by removing the first and last characters. The base case
occurs when the string has one or zero characters, in which case the function returns true.
// Recur for the substring without the first and last characters
return test_palindrome_rec(test_word, start + 1, end - 1);
}
The task involves writing a function to reverse a string using recursion. In each recursive call, the
function swaps the first and last characters of the string, and then reverses the substring that
excludes the first and last characters.
Explanation:
• The base case is when the start index is greater than or equal to the end index, which
indicates that the string has been fully reversed.
• The characters at the start and end positions are swapped in each recursive call.
• The function then calls itself with the substring defined by start + 1 and end - 1 until
the base case is reached.
Conclusion:
In this lab, we implemented recursive functions to solve problems such as checking for
palindromes, finding the GCD, and reversing a string. We learned how recursion works,
including the importance of base cases and memory allocation for recursive function calls. We
also converted iterative solutions to recursive ones and vice versa, gaining a deeper
understanding of both techniques and their respective advantages and disadvantages. Recursion
is a powerful technique for solving problems that can be broken down into smaller sub-problems,
but care must be taken to avoid stack overflow errors by properly defining base cases.