0

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
Yunnosch
  • 26,130
  • 9
  • 42
  • 54
ayetman96
  • 15
  • 4
  • Take a look at [this question](https://stackoverflow.com/questions/3893937/c-array-sorting-tips) – Neijwiert May 16 '18 at 14:17
  • Along with the intended output, you should include the output you are getting instead. – Christian Gibbons May 16 '18 at 14:17
  • 1
    `char words[100];` should be `char *words[100];` – Martin Chekurov May 16 '18 at 14:19
  • @MartinChekurov I think there's more wrong than just that. – Neijwiert May 16 '18 at 14:20
  • `char words[100];` is just a string you need an array of strings for what you have in mind. so maybe `char words[10][100];`. Because the way you do it you will overwrite Brad partly with Matthew giving you BMatthew – Kami Kaze May 16 '18 at 14:21
  • @Neijwiert of course, this is a comment not an answer – Martin Chekurov May 16 '18 at 14:22
  • From what i understood you have to store the names in a two-dimensional array in the first function.In the second one you use an array of pointers and each pointer will point to a name in the 2D array. – Martin Chekurov May 16 '18 at 14:31
  • Review `scanf_s("%s", &words[a]);`. It is missing an argument. A good compiler with its warnings fully enabled would complain and save you time. – chux - Reinstate Monica May 16 '18 at 14:51
  • @MartinChekurov. Could you elaborate on your comments? I have tried multiple changes and have not gotten the appropriate output. Would you be able to show some of your coding for this by any chance? – ayetman96 May 16 '18 at 16:17
  • @MartinChekurov How does that help, `char *words[100];` is just an array of 100 `char *` he still can't store the words... You'd have to do what @KamiKaze suggested. – samuelnj May 16 '18 at 17:22
  • @Samuelnj The program is now telling me that my type char is incompatible with type const char in my if(strlen) {} statement and that it also cannot convert char to const char. I am not trying to do this so I am not sure why I would be getting this error. Also in my printf statements at the end it is saying "%30s can only print type char but I am trying to print type int." I am not trying to print type int I am trying to print my word[a] string. Why would I be getting this error. Again thanks for all of your help. I am really new to coding and not very good at troubleshooting – ayetman96 May 16 '18 at 18:24
  • @samuelnj `char *words[100]` can be used to store 100 words. – Martin Chekurov May 17 '18 at 06:07
  • @MartinChekurov yes, but he'd still have to keep declaring character arrays of some size and then assign them to the pointers in the array, which is a headache. It's much better to have `char words[100][10]` where it's very clear you're trying to store 100 words of size 10, and it's continuous in memory. – samuelnj May 17 '18 at 14:25
  • @samuelnj what if some words have more than 10 letters?`words[0] = malloc(5); words[1] = malloc(20);` makes it possible – Martin Chekurov May 17 '18 at 14:35

1 Answers1

0

You can use qsort to sort the strings by their size

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

#define MAXLEN 25

int compare (const void * a, const void * b) {
    size_t fa = strlen((const char *)a);
    size_t fb = strlen((const char *)b);
    return (fa > fb) - (fa < fb);
}

int main() {

    int num_words;
    scanf("%d", &num_words);

    char word[MAXLEN];


    char input[num_words][MAXLEN];
    for(int i = 0; i < num_words; i++ ) {
        scanf("%s", word);
        strcpy(input[i], word);
    }

    qsort(input, num_words, MAXLEN, compare);
    for(int i = 0; i < num_words; i++)
        printf("%s\n", input[i]);
    return 0;
}

The result sorted by string size:

Input:              Output:

Brad                Brad
Matthew             Megan
Brandon             Brandon
Megan               Melissa
Melissa             Matthew
MiguelD
  • 409
  • 1
  • 7
  • 16
  • I copied your coding over to my coding program (visual studio) and it isn't working for me. I get the error that "input" must have a constant value instead of " num_words." Also it says that scanf and strcpy are unsafe to use... Why would I be getting these errors? Thanks for your comment... It really cleared the question up for me – ayetman96 May 16 '18 at 19:19
  • the first problem you describe i think it has to do with your C compilier version, i did the code in C99 in witch it is possible to create an array when one of the size parameters if given by user input, the other problem i never faced it before so i can´t help, my IDE doesn`t sound any alarm, but check if you imported the string library – MiguelD May 16 '18 at 19:24
  • Thanks Miguel! I just changed the value to an arbitrary number that would probably never be hit and used a piece of code on Microsoft's website that avoids that type of error and now the program works great! I appreciate it! – ayetman96 May 16 '18 at 19:39
  • No problem, if my answer was what you wanted, please mark it as the correct one. – MiguelD May 16 '18 at 19:42