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