Question: Need help how to perform this assignment: a C program that gets a pattern string from command- line arguments, receives multiple lines from the standard

Need help how to perform this assignment: a C program that gets a pattern string from command- line arguments, receives multiple lines from the standard input stream, and finally prints out the lines containing the pattern string.

The program must support the following command-line arguments:

(10 points) -n: prints out the line numbers that contain the pattern string. Please note that line-numbering starts from 1.

(10 points) -x: prints out the lines that do not contain the pattern string (or, excludes the lines containing the pattern string)

(10 points) -s: prints out the lines in alphabetical order (default order is FIFO).

(10 points) -r: prints out the lines in the reverse order (the line that appears last in stdin will appear first in stdout). Please note that you must make sure that user does not use -s and -r flags at the same time. If user wants to use both -s and -r flags, your program must get terminated after giving proper error message to the user.

(10 points) -m: finds the pattern string only if it matches the whole word. You can assume that words are separated by white-space character only.

(5 points) -c: ignores the case when finding the pattern string.

(15 points) -f: prints out the index of the first occurrence of pattern string in each line. Please note that you must make sure that user does not use -f and -x flags at the same time. If user wants to use both -f and -x flags, your program must get terminated after giving proper error message to the user.

(20 points) -p: partially prints out each line containing the pattern.

starter code to modify//

Need help how to perform this assignment: a C program that gets

//CLA: ./find -p apple

//INPUT: I like different types of fruit, especially apple and orange!

//OUTPUT: I like dif...apple...ange!

//CLA: ./find -p apple

//INPUT: I like different types of fruit, especially apple

//OUTPUT: I like dif...apple

//CLA: ./find -p apple

//INPUT: I like apple

//OUTPUT: I like apple

#include

#include

#include

void swap(char* arr[], int i, int j){

void* temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

}

char *strstr_fully_matched(char *haystack, char *needle) {/eedle = "apple"

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';//" apple "

if (!strncmp(haystack, padded_needle + 1, strlen(needle) + 1)) {

return haystack; // needle is at the beginning

}

if ((rv = strstr(haystack, padded_needle)) != NULL) {

return rv + 1; // needle is at the middle.

}

padded_needle[strlen(needle) + 1] = '\0';

if ((rv = strstr(haystack, padded_needle)) != NULL &&

rv[strlen(padded_needle)] == '\0') {

return rv + 1; // needle is at the end.

}

return NULL;

}

void book_s_qsort(char*v[], int left, int right){

int i, last;

if(left >= right)

return;

swap(v, left, left + (right-left)/2);

last = left;

for(i = left+1; i

if(strcmp(v[i],v[left])

swap(v, ++last, i);

}

swap(v, left, last);

book_s_qsort(v, left, last-1);

book_s_qsort(v, last+1, right);

}

void error(char* message){

printf("%s ", message);

}

char* lineptr[1000];

char* strstr_updated(char* haystack, char* needle, int match){

if(match)

return strstr_fully_matched(haystack, needle);

else

return strstr(haystack, needle);

}

int main(int argc, char* argv[]){

int except = 0, number = 0, sort = 0, reverse = 0, match = 0;

if(argc

error("fatal error: too few arguments");

return 1;

}

int i = 1;

while(argc != 2){

char* current_arg = argv[i++];

if(current_arg[0] != '-'){

error("fatal error: illegal CLAs");

return 2;

}

while(*++current_arg){

switch(*current_arg){

case 'x': case 'X':

except = 1;

break;

case 'n': case 'N':

number = 1;

break;

case 's': case 'S':

sort = 1;

break;

case 'r': case 'R':

reverse = 1;

break;

case 'm': case 'M':

match = 1;

break;

default:

error("fatal error: illegal option entered");

return 3;

}

}argc--;

}

char* pattern = argv[i];

printf("pattern is %s, n = %d, s = %d, r = %d, m = %d, x = %d ", pattern, number,

sort, reverse, match, except);

if(reverse && sort){

error("fatal error: can't use -r and -s in the same execution");

return 4;

}

//input stream read operation...

char line[1000];

i = 0;

while (fgets(line, 1000, stdin)) {

if(line[strlen(line) - 1] == ' ')

line[strlen(line) - 1] = '\0';//drop the new line

lineptr[i++] = strdup(line);//malloc to allocate space in heap and strcpy to copy line to heap

}//i represents the no of sentences input by the user

if(!sort && !reverse){

for(int j = 0; j

char* result = strstr_updated(lineptr[j], pattern, match);

char* line_no = (char*) malloc(10);

sprintf(line_no, "%d. ", (j+1));

//if pattern is found and -x doesn't exist

//or if pattern is missing and -x exists

//then print the line in the output

if(result && !except || !result && except)

printf("%s%s ", number? line_no:"", lineptr[j]);

}

}else if(sort){

book_s_qsort(lineptr, 0, i - 1);

for(int j = 0; j

char* result = strstr_updated(lineptr[j], pattern, match);

char* line_no = (char*) malloc(10);

sprintf(line_no, "%d. ", (j+1));

if(result && !except || !result && except)

printf("%s%s ", number? line_no:"", lineptr[j]);

}

}else{//reversed

//assist here as well...

}

return 0;/o error occured!

2 Input/Output of Your Program Your program must accept a sequence of lines followed by an EOF character from standard input stream. Here is an example of program input: I'm very interested in Computer Science This is a test statement I'm in In-N-Out has great burgers in fact Last sentence In [Ctr|] +D The output is given under different combinations of command-line arguments: 3 Submissions You need to submit a .zip file compressing the followings: - all .c and .h files 2 - (10 points) makefile which organizes the compilation process of your program. Use the word "find" to name the executable of your program

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!