Part of the code is implemented already on the questions details you will find what is missing.
The program must support the following command-line arguments:
-n: prints out the line numbers that contain the pattern string. Please note that line-numbering starts from 1. DONE
-x: prints out the lines that do not contain the pattern string (or, excludes the lines containing the pattern string) DONE
-s: prints out the lines in alphabetical order (default order is FIFO). DONE
-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. MISSING
-m: finds the pattern string only if it matches the whole word. You can assume that words are separated by white-space character only. DONE
-c: ignores the case when finding the pattern string. MISSING
-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. MISSING
-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 first occurrence of 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 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 legnth 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. MISSING
Your program must accept a sequence of lines followed by an EOF character from standard input stream.
Make File which organizes the compilation process of your program. Use the word “find� to name the executable of your program. MISSING
Requirements: finish the missing part
This is my own codes #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_LINE_LENGTH 1000
int reverse_flag = 0;
int case_insensitive_flag = 0;
int first_occurrence_flag = 0;
int partial_match_flag = 0;
char *pattern;
void print_error_message(char *msg) {
fprintf(stderr, “%s\n”, msg);
exit(EXIT_FAILURE);
}
void print_usage() {
printf(“Usage: find [-r] [-c] [-f] [-p] pattern\n”);
}
int check_flags() {
int count = 0;
if (reverse_flag) count++;
if (case_insensitive_flag) count++;
if (first_occurrence_flag) count++;
if (partial_match_flag) count++;
return count;
}
void print_reverse(char **lines, int num_lines) {
for (int i = num_lines – 1; i >= 0; i–) {
printf(“%s”, lines[i]);
}
}
int find_pattern(char *line) {
if (case_insensitive_flag) {
char *line_lower = strdup(line);
for (int i = 0; line_lower[i]; i++) {
line_lower[i] = tolower(line_lower[i]);
}
char *pattern_lower = strdup(pattern);
for (int i = 0; pattern_lower[i]; i++) {
pattern_lower[i] = tolower(pattern_lower[i]);
}
char *match = strstr(line_lower, pattern_lower);
free(line_lower);
free(pattern_lower);
if (match) {
return match – line;
} else {
return -1;
}
} else {
char *match = strstr(line, pattern);
if (match) {
return match – line;
} else {
return -1;
}
}
}
void print_first_occurrence(char **lines, int num_lines) {
for (int i = 0; i < num_lines; i++) {
int index = find_pattern(lines[i]);
printf(“%d\n”, index);
}
}
void print_partial_match(char **lines, int num_lines) {
for (int i = 0; i < num_lines; i++) {
char *match = strstr(lines[i], pattern);
if (match) {
int index = match – lines[i];
int pattern_len = strlen(pattern);
int line_len = strlen(lines[i]);
if (line_len <= pattern_len + 15) {
printf(“%s”, lines[i]);
} else {
int start = index – 10;
int end = index + pattern_len + 5;
if (start < 0) {
start = 0;
}
if (end > line_len) {
end = line_len;
}
if (start > 0) {
printf(“… “);
}
for (int j = start; j < index; j++) {
printf(“%c”, lines[i][j]);
}
printf(“… “);
for (int j = index; j < index + pattern_len; j++) {
printf(“%c”, lines[i][j]);
}
printf(“…”);
for (int j = index + pattern_len; j < end; j++) {
printf(“%c”, lines[i][j]);
}
printf(“\n”);
}
}
}
}
int main(int argc, char **argv) {
if (argc < 2) {
print_usage();
exit(EXIT_FAILURE);
for (int i = 1; i < argc; i++) {
if (argv[i][0] == ‘-‘) {
int len = strlen(argv[i]);
for (int j = 1; j < len; j++) {
switch (argv[i][j]) {
case ‘r’:
reverse_flag = 1;
break;
case ‘c’:
case_insensitive_flag = 1;
break;
case ‘f’:
first_occurrence_flag = 1;
break;
case ‘p’:
partial_match_flag = 1;
break;
default:
print_error_message(“Error: Invalid flag”);
break;
}
}
} else {
if (pattern == NULL) {
pattern = argv[i];
} else {
print_error_message(“Error: Invalid input format”);
}
}
}
if (pattern == NULL) {
print_error_message(“Error: Pattern not specified”);
}
if (check_flags() > 1) {
print_error_message(“Error: Multiple flags specified”);
}
char **lines = malloc(MAX_LINE_LENGTH * sizeof(char *));
int num_lines = 0;
char buffer[MAX_LINE_LENGTH];
while (fgets(buffer, MAX_LINE_LENGTH, stdin) != NULL) {
lines[num_lines] = strdup(buffer);
num_lines++;
}
if (reverse_flag) {
print_reverse(lines, num_lines);
} else if (first_occurrence_flag) {
print_first_occurrence(lines, num_lines);
} else if (partial_match_flag) {
print_partial_match(lines, num_lines);
} else {
for (int i = 0; i < num_lines; i++) {
if (find_pattern(lines[i]) != -1) {
printf(“%s”, lines[i]);
}
}
}
for (int i = 0; i < num_lines; i++) {
free(lines[i]);
}
free(lines);
return 0;But it seems not to be accepting inputs and to display the right answer.