Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using the programming language C. In this assignment, you will write a program that gets a pattern string from command line arguments, receives multiple lines

Using the programming language C.

In this assignment, you will write a 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.

1 Program Command Line Arguments

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. To print out each line partially, you need to print the first 10 characters of the line, followed by an ellipsis (. . . ), followed by the pattern followed by another ellipsis, followed by the last five characters of the line. You can remove the first ellipsis if the pattern overlaps the 1 first 10 characters. Also, you can remove the second ellipsis if the pattern overlaps the last five characters. Additionally, you can print the whole sentence if the length of sentence is not greater than the pattern length plus 15. Please note that you must make sure that user does not use -p and -x flags at the same time. If user wants to use both -p and -x flags, your program must get terminated after giving proper error message to the user.

image text in transcribed

starter code

//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  

.

The output is given under different combinations of command-line arguments

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

Step: 3

blur-text-image

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

Database And Expert Systems Applications 33rd International Conference Dexa 2022 Vienna Austria August 22 24 2022 Proceedings Part 1 Lncs 13426

Authors: Christine Strauss ,Alfredo Cuzzocrea ,Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil

1st Edition

3031124227, 978-3031124228

More Books

Students also viewed these Databases questions

Question

2. What, according to Sergey, was strange at this meeting?

Answered: 1 week ago