Question: consider the following incomplete c program that receives multiple lines from stdin, sort them alphabetically, and print them all out in the sorted order. Complete

consider the following incomplete c program that receives multiple lines from stdin, sort them alphabetically, and print them all out in the sorted order. Complete the program so that it allows the user to enter an optional command-line argument "-i" when running the program to ignore the case when sorting the input lines. The default behavior should sort in a case-sensitive manner ```
#include
#include
#include
#define MAX_LINES 5000
#define MAX_LEN 1000
char* lineptr$$\lbrack$$MAX_LINES$$\rbrack$$;
int read_lines(void){
char line$$\lbrack$$MAX_LEN$$\rbrack$$;
int nline =0;//# of lines
while(fgets(line,MAX_LEN, stdin)){
int len = strlen(line);
if(line$$\lbrack$$len-1$$\rbrack$$ =='\
n'){
line$$\lbrack$$len-1$$\rbrack$$ ='\
0';
len--;
}
lineptr$$\lbrack$$nline$$\rbrack$$ =(
) malloc((len +1)*sizeof(char));
if(lineptr$$\lbrack$$nline$$\rbrack$$ ==
)//heap is full, cannot allocate memory!
return -1;
(lineptr\$\$\lbrack\$\$nline++\$\$ rbrack\$\$, line);//a
lternative: lineptr$$\lbrack$$nline++$$\rbrack$$
= strdup(line);
}
}
return nline;
void write_lines(int len){
for(int i =0; i len;i++)
printf("%s
", lineptr$$\lbrack$$i$$\rbr
ack$$);
}
``````
(lineptr$$\lbrack$$nline++$$\rbrack$$, line);//a
lternative: lineptr$$\lbrack$$nline++$$\rbrack$$
= strdup(line);
}
return nline;
}
void write_lines(int len){
for(int i =0; i len;i++)
ack$$);
printf("%s
", lineptr$$\lbrack$$i$$\rbr
}
int compare_strings(const void* first, const voi
d* second){
//returns a negative int if *first *second
//returns a positive int if *first >*second
//returns 0 otherwise!
const char** first_string_pointer =(const c
har**)first;
const char** second_string_pointer =(const
char**)second;
return strcmp(*first_string_pointer, *second
_string_pointer);
}
int compare_strings_ignore_case(const void* firs
t, const void* second){
//ignores case
const char** first_string_pointer =(const cha
r**)first;
const char** second_string_pointer =(const
char**)second;
return strcasecmp(*first_string_pointer, *seco
nd_string_pointer);
}
int main(int arqc,
argv$$[]$$){
int number_of_lines, case_sensitive =1;
if(
>1)
case_sensitive =(strcmp(argv$$\lbrack$$
``````
argv$$[]$$){
int number_of_lines, case_sensitive =1;
ifl
>1)
```
case_sensitive =(strcmp(argv$$\lbrack$$
$$\rbrack$$,"-i")==0)?0: 1;
if((number_of_lines = read_lines())0){
printf("Error: cannot store input stream
in the memory!
");
}
return 1;//error code for low memory
s);
printf("# of lines: %d.
", number_of_line
qsort(lineptr,(size_t)number_of_lines, size
of(char*),
case sensitive?
:
);
write_lines
);
for(int i =0; i number_of_lines;i++)
(lineptr$$\lbrack$$i$$\rbrack$$);//deallocate me
mory
}
consider the following incomplete c program that

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 Programming Questions!