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...