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

C Programming Homework Write a C program project5.c that operates as described b

ID: 3919646 • Letter: C

Question

C Programming Homework

Write a C program project5.c that operates as described below Your program should accept three command line arguments. Argument 1 is the name of an input file that contains a list of key words. Argument 2 is the name of an input file that contains a list of query words. Argument 3 is the name of an output file First your program reads all the key words in the first input file, and it adds each such key word into a linked list that maintains these key words in alphabetical order. This process ends when the end-of-file condition is reached for the key words. Next your program allocates an array of strings, with size of the array equal to the number of key words in the linked list. Then it copies all the key words from the linked list into this array so that the words remain in alphabetical order Finally your program reads each query word from the second input file, and it uses the binary search algorithm to search for each query word within the array. For each query word that is found to appear in the array of key words, your program prints the word and its index to the output file. This process ends when the end-of-file condition is reached for the query words Here is an example Keywords file red green blue yellow cyan magenta white black gray orange pink Keywords array Queries file indigo white violet black purple yellow brown cyan gold magenta silver gray crimson pink maroon orange scarlet red beige green tan Output file white 9 black 0 0: black 1: blue 2: cyarn 3 gray 4: green 5: magenta 6: orange 7: pink 8: red 9: white yellow cyan 2 magenta 5 gray 3 pink 7 orange 6 red 8 green4 blue 1 10 10: yellow

Explanation / Answer

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

struct Node

{

char *data;

struct Node *next;

};

void push(struct Node** head_ref, char *new_data, size_t data_size)

{

// Allocate memory for node

struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));

new_node->data = malloc(data_size);

new_node->next = (*head_ref);

// Copy contents of new_data to newly allocated memory.

// Assumption: char takes 1 byte.

int i;

for (i = 0; i<data_size; i++)

*(new_node->data + i) = *(new_data + i);

// Change head pointer as new node is added at the beginning

(*head_ref) = new_node;

}

int binarySearch(char arr[][11], int l, int r, char* x)

{

if (r >= l)

{

int mid = l + (r - l) / 2;

// If the element is present at the middle

// itself

if (strcmp(arr[mid],x) == 0)

return mid;

// If element is smaller than mid, then

// it can only be present in left subarray

if (strcmp(arr[mid], x) < 0)

return binarySearch(arr, l, mid - 1, x);

// Else the element can only be present

// in right subarray

return binarySearch(arr, mid + 1, r, x);

}

return -1;

}

int main(int argc, char **argv)

{

FILE * fp;

FILE *fout;

char * line = NULL;

size_t len = 255;

int read,sizeOflist;

struct Node *start = NULL;

fp = fopen(argv[0], "r");

if (fp == NULL)

exit(EXIT_FAILURE);

while ((read = fgets(&line, &len, fp)) != -1) {

push(&start, line, len);

sizeOflist++;

}

fclose(fp);

if (line)

free(line);

//Enter into string array

char keywordsArray[11][10];

struct Node*curr = start;

int i = 0;

while (curr != NULL) {

char* data = curr->data;

strcpy(keywordsArray[i++], data);

curr = curr->next;

}

//read input file

fout = fopen(argv[2], "wb");

fp = fopen(argv[1], "r");

if (fp == NULL)

exit(EXIT_FAILURE);

while ((read = fgets(&line, &len, fp)) != -1) {

int index = binarySearch(keywordsArray, 0, sizeOflist, line);

fputs(line,fout);

fputs(index, fout);

fputc(' ', fout);

}

fclose(fp);

if (line)

free(line);

return 0;

}