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

Please help me with this C question. Please read the instructions very carefully

ID: 3734440 • Letter: P

Question

Please help me with this C question. Please read the instructions very carefully too:

Header:

Main.c

.C (write program here; it's currently empty and needs to be filled)

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 reusable/dynamic. 1. Swap function accepting void pointers 2. Recursive reverse list function.

Explanation / Answer

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote