Question: There are two functions that you will need to write for this lab. The first function will swap the value of a string (char *)
There are two functions that you will need to write for this lab. The first function will swap the value of a string (char *) pointer. This swap function will accept two void pointers and swap the values. The Second, will be a recursive function that reverses the order of the strings in the list/array. When you find a solution, you will realize how simple the code in a recursive function can be. We will also use a function pointer for the swap function in the reverse function. This way the code will be more flexible making the program reusable/dynamic.
here's the code I've written so far for recursion.c:
void swapString(void **a, void **b){
void* temp;
temp = *a;
*a = *b;
*b = temp;
}
void recursiveReverse(void **listPtr, int lowIndex, int highIndex, void (*swapFunction) (void **, void **)){
if(lowIndex
swapFunction((&(*listPtr)-lowIndex),(&(*listPtr)-highIndex));
recursiveReverse(listPtr,++lowIndex,--highIndex,swapFunction);
}
else {
return;
}
}
heres the main file im using for the recursion.c file:
int main(int argc, char *argv[]) {
void* a;
void* b;
void* c;
void* d;
char* str1 = malloc(sizeof(char)*10);
str1 = "4";
char* str2 = malloc(sizeof(char)*10);
str2 = "3";
char* str3 = malloc(sizeof(char)*10);
str3 = "2";
char* str4 = malloc(sizeof(char)*10);
str4 = "1";
a = *(char**)&str1;
b = *(char**)&str2;
c = *(char**)&str3;
d = *(char**)&str4;
recursiveReverse(&a, 0, 3, swapString);
printf("Final %s ", *(char**)&str1);
printf("Final %s ", *(char**)&str2);
printf("Final %s ", *(char**)&str3);
printf("Final %s ", *(char**)&str4);
return 0;
}
heres the .h file for it :
*****
* Standard Libraries
*****/
#include
#include
#include
/*****
* Function Prototypes for recursiveReverse.c
*****/
/* Swap the value of two strings */
void swapString(void **a, void **b);
/* This is the recursiveReverse function that will reverse the order of a list */
void recursiveReverse(void **listPtr, int lowIndex, int highIndex, void (*swapFunction) (void **, void **));
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
