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

In this introductory assignment, you will write a complete C program that will p

ID: 3886763 • Letter: I

Question

In this introductory assignment, you will write a complete C program that will parse an input fil... In this introductory assignment, you will write a complete C program that will parse an input file and search for occurrences of a word pattern. The program will output the total number of occurrences and all the lines with the word pattern in it. • Command-Line Arguments Read in the command-line arguments and verify that there are three parameters: (1) the program name, (2) an input file that is the name of the file you will be working with, and (3) the word pattern that you are searching for. If the number of parameters is incorrect, print out a usage message and terminate the program. • File I/O Open the file that was given as a command-line argument to your program. Then, read through the file and print the following information: (1) the total number of lines in the file, (2) the number of characters in the longest line of the file, and (3) the longest line of the file. • Pattern Matching (i.e., grep) Create a data structure that holds all of the information for each occurrence of a word pattern. For example, it should hold the line number and the contents of the line it occurs on. Print all of the information (i.e., the line number and the contents of the line it occurs on), including the total number occurrences of the word pattern. Some assumptions and general rules: • Your program should be case sensitive (i.e., the word pattern “and” is different than the word pattern “AND”). • You may assume that no lines in the file will be longer than 80 characters (81 with the null character). You may assume that the lengths of the filename and word pattern given as command-line arguments are not longer than 15 characters (16 with the null character). • You may not make any assumptions about how many occurrences of the word pattern are in the file, or each line for that matter. Therefore, you may want to use a linked list and allocate memory for each occurrence as you go. You must deallocate the memory when no longer needed. • Add error checking where needed (e.g., after opening file, allocating memory, etc.) and take the appropriate action. Your code should be well documented in terms of comments. For example, good comments in general consist of a header (with your name, course section, date, and brief description), comments for each variable, and commented blocks of code. •

Your program should be named “minor1.c”, without the quotes.

SAMPLE OUTPUT (user input shown ):

$ gcc minor1.c

$ more input1.txt

This life, which had been the tomb of his virtue and of his honour, is but a walking shadow; a poor player, that struts and frets his hour upon the stage, and then is heard no more: it is a tale told by an idiot, full of sound and fury, signifying nothing.

-- William Shakespeare

$ ./a.out input1.txt and Total Number Occurrences of "and" in File: 4

______________________________________

Line 2: tomb of his virtue and of his

Line 5: struts and frets his hour upon

Line 6: the stage, and then is heard

Line 8: idiot, full of sound and fury,

__________________________________
Total Lines in File = 10

Characters Longest Line = 33

Longest line = no more: it is a tale told by an

$ ./a.out input1.txt

his Total Number Occurrences of "his" in File: 4

_____________________________________________________

Line 1: This life, which had been the

Line 2: tomb of his virtue and of his

Line 5: struts and frets his hour upon

_______________________________________

Total Lines in File = 10

Characters Longest Line = 33

Explanation / Answer

Given below is the C program for the question. Output also shown. Hope the answer helped. If it did, please don't forget to rate the answer. Thank you very much.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct Occurence
{
int lineNo;
char lineContents[81];
struct Occurence* next;
};
int count(char *line, char *pattern);
void addToList(struct Occurence** head, int lineNo, char *contents);
void displayList(struct Occurence* head);

int main(int argc, char *argv[])
{
FILE *fp;
char pattern[16];
int lineCount = 0;
int occurences = 0;
int longestLength = 0;
char longestLine[81];
char line[81];
int lineLen ;
int n;
struct Occurence* head = NULL;

if(argc != 3)
{
printf("%s <input_filename> <search_pattern> " , argv[0]);
return 1;
}

fp = fopen(argv[1], "r");
if(fp == NULL)
{
printf("Could not open input filename %s " , argv[1]);
return 1;
}

strcpy(pattern, argv[2]); //copy pattern from command line arguemnt

while(fgets(line, 80, fp))
{
lineCount++;
//check if this is longer than previous longest line
lineLen = strlen(line);
if(lineLen >= longestLength)
{
longestLength = lineLen;
strcpy(longestLine, line);
}

//count the occurences in this line and add to list if any occurnce found
n = count(line, pattern);
if(n != 0)
{
addToList(&head, lineCount, line);
occurences += n;
}
}

fclose(fp);

//display the results
printf("Total number of occurences of "%s" in file : %d ", pattern, occurences );
if(occurences != 0)
{
printf("-------------------------- ");
displayList(head);
printf("-------------------------- ");
}
printf("Total lines in file : %d ", lineCount);
printf("No. of characters in longest line: %d ", longestLength);
printf("Longest line: %s ", longestLine);

}

int count(char *line, char *pattern)
{
int i, j, k;
int len1 = strlen(line);
int len2 = strlen(pattern);
int lastIndex;
int match = 0;
int total = 0;
if(len2 > len1) //can't find a bigger lenght pattern in a smaller line
return 0;

lastIndex = len1 - len2; //the index beyond which searching needs to be stopped
for(i = 0; i <= lastIndex;)
{
match = 1;
for(j = 0, k = 0; j < len2; j++, k++)
{
if(line[i+k] != pattern[j])
{
match = 0;
break;
}
}
if(match == 1)
{
total++;
i += len2; //increment by length pattern
}
else
i++; //next character
}
return total;
}
void addToList(struct Occurence** head, int lineNo, char *contents)
{
struct Occurence* n = (struct Occurence*) malloc(sizeof(struct Occurence));
struct Occurence* last;
if(n == NULL)
{
printf("Error allocating memory. Exiting... ");
exit(1);
}

n->lineNo = lineNo;
strcpy(n->lineContents, contents);
n->next = NULL;

if(*head == NULL)
*head = n;
else
{
last = *head;
while(last->next != NULL)
last = last->next;
last->next = n;
}
}

void displayList(struct Occurence* head)
{
struct Occurence* n = head;

while(n != NULL)
{
printf("Line %d: %s", n->lineNo, n->lineContents);
n = n->next;
}
}

output

Total number of occurences of "his" in file : 4
--------------------------
Line 1: This life, which had been the
Line 2: tomb of his virtue and of his
Line 5: struts and frets his hour upon
--------------------------
Total lines in file : 10
No. of characters in longest line: 33
Longest line: no more: it is a tale told by an

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