Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sorting Words by Length in C

I’m attempting to read words from the command line using argv in C and then sort them in descending order based on their lengths. However, my sorting algorithm is producing unexpected output.

The code I’m using is as follows:

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

int main(int argc, char *argv[]) {

    for (int i = 1; i < argc - 1; i++) {
        for (int j = 1; j < argc - i - 1; j++) {
            if (strlen(argv[j]) < strlen(argv[j + 1])) {
                char temp_word[20];
                strcpy(temp_word, argv[j]);
                strcpy(argv[j], argv[j + 1]);
                strcpy(argv[j + 1], temp_word);
            }
        }
    }

    puts("\n");
    for (int i = 1; i < argc; i++) {
        printf("%s ", argv[i]);
    }

    return 0;
}
gcc test.c -o test
./test I put this words


puwordI wordI I  % 

I read the words from the command line using argv and then attempt to sort them by length. Unfortunately, the output is corrupted. I suspect there might be an issue with my sorting logic or how I’m handling the command line arguments. Could someone please review my code and provide guidance on how to correctly sort the words by length when reading them from argv?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Any help or suggestions would be greatly appreciated.

Thank you!

>Solution :

There are two issues with your code. First, you are trying to swap the content of the strings in the argv array: this won’t work, because the buffer assigned the shorter of two strings to be swapped will not be large enough to hold the ‘replacement’ from the longer, and will lead to undefined behaviour because of buffer overrun. Instead, just swap the actual pointers.

Second, you are not testing the whole array; take out the - 1 from the limits in the two for loops.

Here’s a corrected version of your code:

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

int main(int argc, char* argv[]) {

    for (int i = 1; i < argc; i++) { // Use "argc" (not argc - 1)
        for (int j = 1; j < argc - i; j++) { // Similarly, no -1 here!
            if (strlen(argv[j]) < strlen(argv[j + 1])) {
                char* temp_word;
                temp_word = argv[j];
                argv[j] = argv[j + 1];    // Just swap the pointers,
                argv[j + 1] = temp_word;  // not the string contents
            }
        }
    }

    puts("\n");
    for (int i = 1; i < argc; i++) {
        printf("%s ", argv[i]);
    }

    return 0;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading