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

I need to make a program that searches for every occurrence of a word within a f

ID: 3731301 • Letter: I

Question

I need to make a program that searches for every occurrence of a word within a file and store that information about each occurrence in a linked list to be printed out. Below I have my linkedList code and header. Also below is some specs. Thank you!

________________________________________________________________________________

This is the .c linked list with functions

#include "wordList.h"

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

Node *addToTail(Node *tail, char *line, int lineNum, int wordNum)

{

       Node *temp = (Node*)malloc(sizeof(Node));

       strcpy(temp->line, line);

       temp->lineNum = lineNum;

       temp->wordNum = wordNum;

       temp->next = NULL;

       if (tail == NULL)

              return temp;

       else

              tail->next = temp;

              return temp;

       return 0;

}

Node *rmFromHead(Node *head, char *line, int *lineNum, int *wordNum)

{

       if (head == NULL)

              return NULL;

       strcpy(line, head->line);

       *lineNum = head->lineNum;

       *wordNum = head->wordNum;

       if (head->next == NULL)

              return NULL;

       else

              head = head->next;

              return head;

       return 0;

}

void printList(Node *head)

{

       Node *temp = head;

       while (temp != NULL)

       {

              printf("Node: -line: %s -lineNum: %d -wordNum: %d ", temp->line, temp->lineNum, temp->wordNum);

              temp = temp->next;

       }

       return;

}

________________________________________________________________________________________________________________

This is header file

#define MAXLINE 101        /* The maximum length of a line, plus one. */

/**

* Represents a node in a linked list of word occurrences.

*/

typedef struct Node {

       char line[MAXLINE];    /* The line in which the word occurs */

       int lineNum;           /* The number of that line within the file */

       int wordNum;           /* The position of the word within that line */

       struct Node *next;     /* The next node in the list */

} Node;

Node *addToTail(Node *, char *, int, int);

Node *rmFromHead(Node *, char *, int *, int *);

void printList(Node *);

Part 3: Command Line Arguments and File I/O Develop a program that accepts, as command line arguments, a word and the name of a file. This program should search for every occurrence of that word within that file, storing information about each occurrence in a linked list so that they can be printed out later Additionally, your program should find the longest line in the file (regardless of whether or not it contains the specified word) and count the number of lines in the file Hint: Develop incrementally. Begin by making sure you can open a file, then read and echo each of its lines. You can use fgets to read single line from a file and strtok to iterate over the words in a line.] Requirements: Write your program in a file named "myGrep.c" .Before it begins its search, your program must verify and echo its command line arguments. . Your string comparisons may be case-sensitive. . You may assume that no line (or word) will be longer than 100 characters. . You may not make any assumptions about how many times the word occurs in a line or in the file . You must read through the file exactly once . You must store each occurrence of the word in a linked list, using the functions in wordList.c and the definitions in wordList.h . Store the line in which the word occurs . Store the number of that line within the file. Store the position of the word within that line. Both the lines in the file and the words in a line are numbered from 0 . You must free any memory that you dynamically allocate.

Explanation / Answer

// File Name: wordList.h
#define MAXLINE 101 /* The maximum length of a line, plus one. */
/**
* Represents a node in a linked list of word occurrences.
*/
// Creates a structure for node
typedef struct Node
{
char line[MAXLINE]; /* The line in which the word occurs */
int lineNum; /* The number of that line within the file */
int wordNum; /* The position of the word within that line */
struct Node *next; /* The next node in the list */
} Node; // End of structure

// Prototype of functions
Node *addToTail(Node *, char *, int, int);
Node *rmFromHead(Node *, char *, int *, int *);
void printList(Node *);

------------------------------------------------------------------

// File Name: wordList.cpp
#include "wordList.h"
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

/*
Function to add a node at tail.
Parameters:
*tail: pointer type node pointing to head
*line: line that contains string read from the file
lineNum: line number where the word found
wordNum: position of the word in the line
*/
Node *addToTail(Node *tail, char *line, int lineNum, int wordNum)
{
// Creates a temporary node dynamically
Node *temp = (Node*)malloc(sizeof(Node));
// Copy the contents of line and stores it in temporary nodes line
strcpy(temp->line, line);
// Stores the line number in structure temp's line number
temp->lineNum = lineNum;
// Stores the word position in structure temp's word number
temp->wordNum = wordNum;
// Sets next position of the temp to NULL
temp->next = NULL;
// Checks if the tail is NULL then it is the first node
if (tail == NULL)
// Return the temp node
return temp;
// Otherwise, it is not the first node
else
// Stores the address of temp in tail next position
tail->next = temp;
// Returns tail
return tail;
return 0;
}// End of function

/*
Function to remove a node at tail.
Parameters:
*head: pointer type node pointing to head
*line: line that contains string read from the file
lineNum: line number where the word found
wordNum: position of the word in the line
*/
Node *rmFromHead(Node *head, char *line, int *lineNum, int *wordNum)
{
// Checks if the head is NULL no node available
if (head == NULL)
// Return NULL
return NULL;
// Copy the contents of temporary node's data and stores it in the parameter variables
strcpy(line, head->line);
*lineNum = head->lineNum;
*wordNum = head->wordNum;

// Checks if the head next is NULL then it is the last node to remove
if (head->next == NULL)
// Return NULL
return NULL;
// Otherwise, it is not the last node
else
// head is pointing to next node of the head
head = head->next;
// Return head
return head;
return 0;
}// End of function

// Function to print the list
void printList(Node *head)
{
// Temporary node points to head
Node *temp = head;
// Loops till end of the list
while (temp != NULL)
{
// Displays node information
printf(" Node: -line: %s -lineNum: %d -wordNum: %d ", temp->line, temp->lineNum, temp->wordNum);
// Move to next node
temp = temp->next;
}// End of while
return;
}// End of function

----------------------------------------------------------------

// File Name: FileSplitWord.c
#include<stdio.h>
#include "wordList.cpp"
// main function definition
void main()
{
// Creates a head node and initializes to NULL
Node *head = NULL;
// Declares an array to store the line read from the file
char str[101];
// Declares an array to store the copy of the line read from the file
char org[101];
// Declares an array to store word
char word[101];
// Character pointer to store the split words
char *ptr;
// Counter for line number and word position
int lineNum = 0;
int wordPos = 0;
// Opens the file for reading
FILE * fp = fopen("StringData.txt", "r");
// Checks if file pointer fp is NULL then display error message
if(fp == NULL)
{
printf("ERROR: Cannot open input file ");
return;
}// End of if condition
// Accept a word from the user
printf(" Enter a word: ");
gets(word);
// Loops till not end of file
while(!feof(fp))
{
// Increase the line number by one
lineNum++;
// Read data from file and stores it in str
fgets(str, 101, fp);
// Creates a copy of str
strcpy(org, str);
// Split our findings around the " "
ptr = (char*)strtok(str, " ");
// Initializes wordPos to zero for each line
wordPos = 0;
// Loops till ptr is not NULL
do
{
// Increase the word position counter by one
wordPos++;
// and keep splitting
ptr = (char*)strtok(NULL, " ");
// Checks if the ptr contents and word contents are same
if(strcasecmp(ptr, word) == 0)
// Calls the function to add the line of string, line number, word position in a node
head = addToTail(head, org, lineNum, wordPos);
}while(ptr != NULL); // End of do - while. No more string available
}// End of outer while loop
// Calls the function to display the list information
printList(head);
// Close the file
fclose(fp);
}// End of main function

-----------------------------------------------------------------------------

File StringData.txt contents:

demo file, this is to split each word.
check this line.

Sample Output:

Enter a word: this

Node:
-line: demo file, this is to split each word.

-lineNum: 1
-wordNum: 2


Node:
-line: check this line.
-lineNum: 2
-wordNum: 1