Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Find.c #includefind.h char * lineptr [ MAX _ LINES ] ; int readlines ( ) { int len, nlines; char * p , line

Find.c
#include"find.h"
char*lineptr[MAX_LINES];
int readlines(){
int len, nlines;
char*p, line[MAX_LEN];
nlines =0;
while(fgets(line, MAX_LEN, stdin)){
len = strlen(line);
if(nlines >= MAX_LINES ||(p = malloc(len +1))== NULL)
return -1;
else{
if(line[len -1]=='
')
line[len -1]='\0';
strcpy(p, line);
lineptr[nlines++]= p;
}
}
return nlines;
}
void error(int error_code){
printf("find: fatal error - Illegal usage. Error code: %d. Usage: \"%s\"
",
error_code, "find [-n][-x][-s][-r][-m][-c][-f][-p] pattern");
exit(error_code);
}
flags set_flags(int argc, char** argv){
flags option =0
for(int i =1; i argc -1;i++){
if(argv[i][0]!='-')
error(2);
for(int j =1; argv[i][j]!='\0';j++)
switch(argv[i][j]){
case 'n':case 'N':
option |= NUMBERED;
break;
case 'x':case 'X':
option |= EXCEPT;
break;
case 's':case 'S':
option |= SORTED;
break;
case 'r':case 'R':
option |= REVERSED;
break;
case 'm':case 'M':
option |= MATCHED;
break;
case 'c':case 'C':
option |= CASE;
break;
case 'f':case 'F':
option |= FIRST;
break;
case 'p':case 'P':
option |= PARTIAL;
break;
default:
error(3);
}
}
return option;
}
int main(int argc, char** argv)
{
if(argc 2)
error(1);
char* pattern = strdup(argv[argc -1]);
flags option = set_flags(argc, argv);
if((option & REVERSED) && (option & SORTED))
error(4);
int nlines = readlines();
if(option & SORTED)
quicksort(lineptr,0, nlines -1);
char initial[10]="";
for(int i =0; i nlines;i++){
if(option & NUMBERED)
sprintf(initial,"%d.", i +1);
char* first_occurrence = strstr_w_option(lineptr[i], pattern, option);
if(((option & EXCEPT)!=0)!=(first_occurrence != NULL))
printf("%s%s
", initial, lineptr[i
}
return 0;
}
Find.h
#include
#include
#include
extern char*lineptr[];
#define MAX_LINES 1000
#define MAX_LEN 1000
typedef enum{
NUMBERED =1,
EXCEPT =11,
SORTED =12,
REVERSED =13,
MATCHED =14,
CASE =15,
FIRST =16,
PARTIAL =17,
}flags;
char* strstr_w_option(char* haystack, char* needle, flags option);
void quicksort(char*str[], int left, int right);
util.c
#include"find.h"
static char *strstr_fully_matched(char *haystack, char *needle){
char *rv;
char padded_needle[strlen(needle)+3];
padded_needle[0]='';
strcpy(padded_needle +1, needle);
padded_needle[strlen(needle)+1]='';
padded_needle[strlen(needle)+2]='\0'
if(!strcmp(needle, haystack))
return haystack
if (!strncmp(haystack, padded_needle +1, strlen(needle)+1)){
return haystack;
}
if ((rv = strstr(haystack, padded_needle))!= NULL){
return rv +1;
}
padded_needle[strlen(needle)+1]='\0';
if ((rv = strstr(haystack, padded_needle))!= NULL &&
rv[strlen(padded_needle)]=='\0'){
return rv +1;
}
return NULL;
}
static void to_lower(char* input){
for(int i =0;i strlen(input);i++)
input[i]= tolower(input[i]);
}
char* strstr_w_option(char* haystack, char* needle, flags option){
if(option & CASE){
to_lower(haystack = strdup(haystack));
to_lower(needle = strdup(needle));
if(option & MATCHED)
return strstr_fully_matched(haystack, needle);
else
return strstr(haystack, needle);
}else if(option & MATCHED)
return strstr_fully_matched(haystack, needle);
else
return strstr(haystack, needle);
}
static void swap(void** v, int i, int j){
void* temp = v[i];
v[i]= v[j];
v[j]= temp;
}
void quicksort(char*str[], int left, int right){
int i, last;
if(left >= right)
return;
swap(str, left, left +(right-left)/2);
last = left;
for(i = left +1; i = right;i++)
if(strcmp(str[i],str[left])0)
swap(str,++last, i);
swap(str, left, last);
quicksort(str, left, last-1);
quicksort(str, last+1, right);
}
My professor told me when I ask him for help and clarification he was traveling and couldnt and to reach out to a teachers assistant but they wont answer anyone in my class. I need help on this assignment I have given my code and dont know what the issue is.
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro Oracle Fusion Applications Installation And Administration

Authors: Tushar Thakker

1st Edition

1484209834, 9781484209837

More Books

Students also viewed these Databases questions

Question

What steps would you take to make a meeting more productive? [LO-3]

Answered: 1 week ago

Question

What are the skills you need to be an effective listener? [LO-4]

Answered: 1 week ago