Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

There are two functions that you will need to write for this lab. The first func

ID: 3710814 • Letter: T

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 *) 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 <stdio.h>

#include <string.h>

#include <stdlib.h>

/*****

* 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 **));

Explanation / Answer

ANS:-

PROGRAM:-

***** code for swap and reverse of the list **********

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct node{
   int data;
   struct node *next;
};

/* Swaps strings by swapping pointers */
void swapString(char **str1, char **str2)
{
char *temp = *str1;
*str1 = *str2;
*str2 = temp;
}

struct node *recursiveReverse(struct node *list)
{
    struct node *revHead;
    if (list == NULL || list->next == NULL)
    {
        return list;
    }

    revHead = recursiveReverse(list->next);
    list->next->next = list;
    list->next = NULL;

    return revHead;
}

// creating linked list
struct node *create(struct node *root){

int x;
struct node *newnode,*ptr;
newnode = (struct node *)malloc(sizeof(struct node*));
printf("Enter data: ");
scanf("%d",&x);
root=newnode;
newnode->data = x;
newnode->next = NULL;
while(x!=0){
     printf("Enter 0 to exit: ");
     newnode = (struct node *)malloc(sizeof(struct node*));
     ptr=root;
     while(ptr->next != NULL){
        ptr=ptr->next;
     }
     scanf("%d",&x);
     newnode->data=x;
     ptr->next=newnode;
     newnode->next=NULL;
}
ptr->next=NULL;
return root;
}

// printing list data
void printData(struct node *root){
struct node *ptr;
ptr=root;
while(ptr){
        printf("%d",ptr->data);
        ptr=ptr->next;
     }
}

int main()
{
int size;
char *str1 = "geeks";
char *str2 = "forgeeks";
swapString(&str1, &str2);
printf("str1 is %s str2 is %s ", str1, str2);
struct node *root=NULL,*ptr;
printf("Enter elements into linkedlist ");
root = create(root);
printf(" list data: ");
printData(root);
printf(" Reverse of the list: ");
struct node *head = recursiveReverse(root);
printData(head);
printf(" ");
return 0;
}

******** code for reverse of the given string (recursive) ***********

#include<stdio.h>
#include<string.h>

// Recursive method to reverse the string

void reverse(char [], int, int);


int main()
{
int size;
char name[20] = "reversestring";
size = strlen(name);
reverse(name, 0, size - 1);
printf("reverse of the string is %s ",name);
return 0;
}

void reverse(char str[], int index, int size)
{
    char temp;
    temp = str[index];
    str[index] = str[size - index];
    str[size - index] = temp;
    if (index == size / 2)
    {
        return;
    }
    reverse(str, index + 1, size);
}