Answer:
See explaination
Step-by-step explanation:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void swap(char *arr[], int i, int minIdx)
{
char temp[30];
strcpy(temp, arr[i]);
strcpy(arr[i], arr[minIdx]);
strcpy(arr[minIdx], temp);
}
void sort(char *arr[], int n)
{
for (int i = 0; i < n - 1; i++)
{
int minIdx = i;
int minV = 10000;
char min[30];
strcpy(min, arr[i]);
for (int j = i + 1; j < n; j++)
{
int a = strcmp(min, arr[j]);
if (a < 0)
{
strcpy(min, arr[i]);
minIdx = j;
}
}
swap(arr, i, minIdx);
}
}
void print(char *arr[], int n)
{
for (int i = 0; i < n; i++)
printf("%s", arr[i]);
}
void read(char *arr[], int *n)
{
FILE *file = fopen("strings.dat", "r");
char line[30];
*n = 0;
printf("Reading from file...\\");
while (fgets(line, 30, file))
{
strcpy(arr[(*n)++], line);
}
print(arr, *n);
}
int main(int argc, char const *argv[])
{
char *arr[50];
for (int i = 0; i < 50; i++)
arr[i] = (char *)malloc(sizeof(char) * 30);
int len;
int n = 0;
read(arr, &n);
printf("\\-------------------------\\After sorting\\");
sort(arr, n);
print(arr, n);
}