Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C program Input: A list of strings, separated by a semicolon, ending by EOF. Ple

ID: 3599848 • Letter: C

Question

C program

Input: A list of strings, separated by a semicolon, ending by EOF. Please note that: Some strings have “new line” (‘ ’ <> )

Output:The list of strings in the ascending order of size of strings, separated by a “new line” (‘ ’<> )

Your code should be as the follows:

void get_strings(char** str): the function is to read a list of strings from the input file. The read list of strings is placed in the pointer str

void sort_strings(char** str): this function is to ascendingly sort strings placed in the pointer str.

void show_strings(char** str): this function is to show strings on the “stdout”. The pointer str points to the list of strings need to be printed out

sample input: abc;def;1234;12

output:

sample input: ;;;;

output:

Explanation / Answer

#include<stdio.h>
#include<conio.h>
#include<string.h>

int get_strings(char** str)
{

int i=0, len = 0;
char* str1;
FILE *fptr;
fptr = fopen("stringinput.txt","r");
fgets(str1, 20, (FILE*)fptr);
*str[i] = strtok(str1, ";");
while(*str != NULL)
{
*str[++i] = strtok(NULL, ";");
}
len = i;
fclose(fptr);
return len;
}

void sort_strings(char** str, int len)
{
int i;
char *str1;
for(i = 0; i < len; i++)
{
if(strlen(*str[i]) > strlen(*str[i+1]))
{
strcpy(str1, *str[i]);
strcpy(*str[i], *str[i+1]);
strcpy(*str[i+1], str1);
}
}
}

void show_strings(char** str, int len)
{
int i;
for(i =0; i< len; i++)
{
printf("%s ",*str[i]);
}
}
void main()
{
char** s;
int len = get_strings(s);
sort_strings(s, len);
show_strings(s, len);
getch();

}