In this program, I get an integer number from the user, for example 3, and the user enters 3 strings. Then I should find the longest string, and print it in reverse.
But I think that my insertion sort code has a problem, and I can't find it.
This is the complete code :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void fatal(const char *msg)
{
fprintf(stderr, "%s\n", msg);
exit(EXIT_FAILURE);
}
int main(void)
{
int n;
printf("How many strings do you want to enter? ");
if (1 != scanf("%d", &n) || 1 > n || n > 32)
fatal("Invalid size specified.");
char strings[n][64];
for (int i = 0; i < n; i++) {
printf("Please enter string #%d: ", i + 1);
if (1 != scanf("%63s", strings[i]))
fatal("Invalid string input.");
}
char selected[100];
int o;
int j;
for (j = 1; j < n; j++)
{
strcpy(selected, strings[j]);
o = j - 1;
while ((j >= 0) && (strcmp(selected, strings[o]) > 0))
{
strcpy(strings[o+1], strings[o]);
o--;
}
strcpy(strings[o + 1], selected);
}
puts(strrev(strings[n-1]));
}
Insertion sort part:
char selected[100];
int o;
int j;
for (j = 1; j < n; j++)
{
strcpy(selected, strings[j]);
o = j - 1;
while ((j >= 0) && (strcmp(selected, strings[o]) > 0))
{
strcpy(strings[o+1], strings[o]);
o--;
}
strcpy(strings[o + 1], selected);
}
If we modify your code slightly to simply print the
stringsarray after sorting, we can see that not all has gone according to plan.With some slight modifications, we can properly implement selection sort and get the right order.
We must iterate
n-1times over the array. On each iteration we'll move the shortest element forward, meaning we can consider this sorted. On each iteration an iteration of the remaining array must be performed to find the index of the shortest element.We can then use a series of calls to
strcpy(usingchar temp[100]as a temp buffer) to swap the strings.You might also use a struct to "cache" each strings length, preventing repeated calls to
strlen.