0

I'm trying to sort a case sensitive array of words words[number of words][number of letters] alphabetically by strcmp using case insensitive array with same words. When I sort case insensitive array using case insensitive array and print it, it works, but if I try to sort case sensitive array using case insensitive array - it does not. Below are the blocks of code I am using to do this.


char cs_sns[20][20];
///scan here as you wish/////
char cs_ins[20][20];

for(int i = 0; i < 20; i++)
{
    strcpy(cs_ins[i], cs_sns[i]);
}

//////////////////CONVERT///////////////////////////
for(int i = 0; i < 20; i++)
{
    strlwr(cs_ins[i]);
}

 /////////////////SORT_CASE_SENSITIVE_BY_LOWERCASE/////////////////////


    for (int j = 0; j < 20; j++)
    {
        for (int i = 0; i < 20-1; i++)
        {
            int x = (strcmp(cs_ins[i], cs_ins[i+1]) > 0); 
            if (x == 1)
            {
                char temp[20];
                strcpy(temp, cs_sns[i]);
                strcpy(cs_sns[i], cs_sns[i+1]);
                strcpy(cs_sns[i+1], temp);
            }
        }
    }

/////////////////////SORT_LOWERCASE_BY_LOWERCASE/////////////////
    for (int j = 0; j < 20; j++)
    {
        for (int i = 0; i < 20 - 1; i++)
        {
            int x = (strcmp(cs_ins[i], cs_ins[i+1]) > 0); 
            if (x == 1)
            {
                char temp[20];
                strcpy(temp, cs_ins[i]);
                strcpy(cs_ins[i], cs_ins[i+1]);
                strcpy(cs_ins[i+1], temp);
            }
        }
    }

    ///////////////PRINT/////////////////

    for (int i = 0; i < 20; i++)
    {
        printf("%s\n", cs_sns[i]);       //////////swap with cs_ins[i] and see what happens
    }

Below is the stolen function strlwr to convert to lowercase.

///////////define strlwr/////////////////////

char *strlwr(char *str)
{
    unsigned char *p = (unsigned char *)str;

    while (*p) 
    {
        *p = tolower((unsigned char)*p);
        p++;
    }

    return str;
}

You'll need to include ctype.h for tolower.

What is it that I am missing? Thank you.

Edit: Can be solved by stricmp, here is a link: C99 remove stricmp() and strnicmp()?

Still curious tho...

1 Answers1

2

The problem occurs because you are trying to sort your case-sensitive array according to the case-insensitive array, while NOT CHANGING the insensitive array.

This may work:

 /////////////////SORT_CASE_SENSITIVE_BY_LOWERCASE/////////////////////


for (int j = 0; j < 20; j++)
{
    for (int i = 0; i < 20-1; i++)
    {
        int x = (strcmp(cs_ins[i], cs_ins[i+1]) > 0); 
        if (x == 1)
        {
            char temp[20];
            strcpy(temp, cs_sns[i]);
            strcpy(cs_sns[i], cs_sns[i+1]);
            strcpy(cs_sns[i+1], temp);
            //swap elements in cs_ins[] as well
            strcpy(temp, cs_ins[i]);
            strcpy(cs_ins[i], cs_ins[i+1]);
            strcpy(cs_ins[i+1], temp);
        }
    }
}

Thus applying the changes to the insensitive array as well.

Of course, just using stricmp would be a much simpler solution, as Monica has stated.

wookiekim
  • 1,156
  • 7
  • 20
  • Yep, that's it. Thank you! – Pierre Rigondeaux Dec 14 '19 at 16:12
  • On POSIX systems, `stricmp()` is not necessarily available (it isn't part of the POSIX standard), but [`strcasecmp()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/strcasecmp.html) found in [``](http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/strings.h.html) is the alternative. Of course, `strlwr()` is not a POSIX function either. – Jonathan Leffler Dec 14 '19 at 17:08