0% found this document useful (0 votes)
18 views4 pages

Lab 07 - BILAL

Uploaded by

eshanaliprivate
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views4 pages

Lab 07 - BILAL

Uploaded by

eshanaliprivate
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab 07 - Implementation of Recursion

Submitted By; Muhmmad Bilal


Reg; FA23-BCE-061
Course: Data Structures
Instructor: Dr. Omer Ahmed
Due Date: December 04, 2024
In-Lab Task 01: Convert the Iterative Function to a Recursive One

Code Listing 1: Iterative Palindrome Test

bool test_palindrome_itr(char * test_word)


{
int length = strlen(test_word) - 1;
int i;
for(i = 0; i <= (length / 2); i++)
{
if((*(test_word + i)) != (*(test_word + length - i - 1)))
break;
}
i--;
return (i == (length / 2));
}

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.

Recursive Palindrome Function:

bool test_palindrome_rec(char * test_word, int start, int end)


{
// Base case: If the string has one or zero characters
if (start >= end)
return true;

// If the first and last characters do not match


if (test_word[start] != test_word[end])
return false;

// Recur for the substring without the first and last characters
return test_palindrome_rec(test_word, start + 1, end - 1);
}

In-Lab Task 02: Convert the Recursive Function to an Iterative One

Code Listing 2: Recursive GCD Function

int GCD_rec(int x, int y)


{
int rem;
rem = x % y;
if(rem == 0)
return y;
else
return (GCD_rec(y, rem));
}
Iterative GCD Function: To convert the recursive GCD function into an iterative one, we can
use a while loop to compute the GCD. The iterative approach replaces the recursive call with a
loop that continues until the remainder becomes zero.

Iterative GCD Function:

int GCD_iter(int x, int y)


{
int rem;
while (y != 0)
{
rem = x % y;
x = y;
y = rem;
}
return x;
}

Post-Lab Task: Reverse a String Using Recursion

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.

Recursive String Reversal:

void reverse_string_rec(char *str, int start, int end)


{
// Base case: If the string has one or zero characters
if (start >= end)
return;

// Swap the characters


char temp = str[start];
str[start] = str[end];
str[end] = temp;

// Recur for the substring


reverse_string_rec(str, start + 1, end - 1);
}

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.

You might also like