# Thread: Problem sorting an array of strings

1. ## Problem sorting an array of strings

Hi!!
Can anybody tell me what wrong with this code, it is supposed to classify the strings alphabetically and display them after...
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 24
#define STR_NUMBER 5

int main(){
char str[STR_NUMBER][SIZE];
char temp[SIZE];
int i,j;
for (i = 0 ; i < STR_NUMBER ; i++)
gets(str[i]);
int n = 0;
for (j = 0 ; j < STR_NUMBER - 1 ; j++){
n = strcmp(str[j],str[j+1]);
if (n>0){
strcpy(temp,str[j]);
strcpy(str[j],str[j+1]);
strcpy(str[j+1],temp);
}
}

printf("\n\nthe array of %d strings sorted:\n\n",STR_NUMBER);
for (int i = 0 ; i < STR_NUMBER ; i++)
puts(str[i]);
system("PAUSE");
return(0);```
Simple input:
a
r
v
g
t
Output:
a
r
g
t
v

Which is totally wrong!!

2. Sorry but i guess J should go up to STR_NUMBER not included so
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 24
#define STR_NUMBER 5

int main(){
char str[STR_NUMBER][SIZE];
char temp[SIZE];
int i,j;
for (i = 0 ; i < STR_NUMBER ; i++)
gets(str[i]);
int n = 0;
for (j = 0 ; j < STR_NUMBER; j++){
n = strcmp(str[j],str[j+1]);
if (n>0){
strcpy(temp,str[j]);
strcpy(str[j],str[j+1]);
strcpy(str[j+1],temp);
}
}

printf("\n\nthe array of %d strings sorted:\n\n",STR_NUMBER);
for (int i = 0 ; i < STR_NUMBER ; i++)
puts(str[i]);
system("PAUSE");
return(0);
}```
Still, there is a mistake..

3. Anyone

4. Code:
```#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SIZE 1024

void Sort(char str[][SIZE], size_t strCount);
int main(int argc, char* argv[])
{
char strList[][SIZE] = {"ZZZZZ", "BBB", "AAA", "MMMM"};

Sort(strList, 4);

for(int i = 0; i < 4; ++i)
printf("&#37;s\n", strList[i]);
return 0;
}

//Simple selection sort
void Sort(char str[][SIZE], size_t strCount)
{
//Loop through all the elements
for(unsigned int i = 0; i < strCount; ++i)
{
//Set smallest to current element
char* smallest = str[i];

//Get the smallest string (starting after current element)
for(unsigned int ii = i + 1; ii < strCount; ++ii)
{
if(strcmp(str[ii], smallest) < 0)
smallest = str[ii];
}

char temp[sizeof(*str)];
//Copy smallest into temp
strncpy(temp, smallest, sizeof(*str));
//Swap current element into the smallest
strncpy(smallest, str[i], sizeof(*str));
//Swap the temp into the current element
strncpy(str[i], temp, sizeof(*str));
}
}```

5. What was that size_t...?? I just know the commonly used data types, we didn't cover typedef or structures if it is the case here!!
Probably not...And my question was what wrong with my code, copying that code below is not very instructive!!

6. Originally Posted by Leojeen
What was that size_t...?? I just know the commonly used data types, we didn't cover typedef or structures if it is the case here!!
Probably not...And my question was what wrong with my code, copying that code below is not very instructive!!
I've added many comments for you to read in the sorting algorithm. size_t is a typedef of "unsigned int" and is defined in the standard c headers so you can just replace "size_t" with "unsigned int" if you want. No one is going to give you a better answer than a fully commented working sorting function. Just compare mine to yours.

7. Ok, good then, thanks

8. To be pedantic (which is always fun, isn't it?), size_t is not necessarily an unsigned int. It's an unsigned integer type, possibly of a different size than int.