I have been tasked with solving the following question using an array of pointers:
Write two prototypes for a function that orders a list of strings according to string length—shortest to longest. In the first, the function should expect an input/output argument that is a two-dimensional array of characters in which strings have at most STRSIZ characters. In the second, the function should expect an input/output argument that is an array of pointers.
I currently have wrote this coding for it and cannot seem to tell why it isn't working properly for me:
int number;
int a;
int b;
char placeholder;
char words[100];
char wordscopy[100];
printf("Enter the amount of words or names you wish to sort (0 - 100) :\n");
scanf_s("%d", &number);
printf("Enter names or words on separate lines\n");
for (a = 0; a < number; ++a)
scanf_s("%s", &words[a]);
for (a = 0; a < number; ++a)
words[a] = wordscopy[a];
for (a = 0; a < number; ++a)
{
for (b = a + 1; b < number; ++b)
{
if (strlen(wordscopy[a]) < strlen(wordscopy[b]))
{
placeholder = wordscopy[a];
wordscopy[a] = wordscopy[b];
wordscopy[b] = placeholder;
}
}
}
printf("\n\n%-30s%5c%-30s\n\n", "Original Order", ' ',"Least Letters to Most Letters");
for (a = 0; a < number; ++a)
printf("%-30s%5c%-30s\n", words[a], ' ', wordscopy[a]);
printf("\n\n");
return 0;
I am fairly new to coding and I can't seem to find out why the program is not working for me.
The output should be:
Original Order Least Letters to Most Letters
Brad Brad
Matthew Megan
Brandon Brandon
Megan Matthew
Melissa Melissa