I have problem to sort an array of string by length loaded from txt file.
So I read from the file line by line and put the strings into an array, after that I sort that array by the length of the string, but I get a strange output of the array stream.
The problem is that the program is sorting an array of strings, but one of the strings is pasted on top of another.
Example:
The data in the file I'm reading from:
X&Y
X|Y
!X
(X|Y)|Z
(X&Y)|Z
(X&Y)&Z
(X&Y)|Z&(A|B
((X|Y)|Z)&((A|B)|(C&D))
(X&Y)|(Z&(A|B))
(A|B)&(!C)
A|(B&(C&(D|E)))
((X|Y)|(Z&(A|B)))|((C&D)&(D|E))
(A|B)|(C&D)&(D|E)
!A&(B|C)
(A|B)|(C|D)&(D
Content of the array after sorting in ascending order:
!X
X|Y
X&Y
(X|Y)|Z
(X&Y)|Z
(X&Y)&Z
!A&(B|C)
(A|B)&(!C)
(X&Y)|Z&(A|B
(A|B)|(C|D)&(DA|(B&(C&(D|E))) //Here' is problem ! (A|B)|(C|D)&(D and A|(B&(C&(D|E))) are concatenated?
(X&Y)|(Z&(A|B))
(A|B)|(C&D)&(D|E)
((X|Y)|Z)&((A|B)|(C&D))
((X|Y)|(Z&(A|B)))|((C&D)&(D|E))
Here is the code:
//Sort function
void sort(char str[][MAXLEN], int number_of_elements) {
int d, j;
char temp[100];
for (d = 0; d < number_of_elements - 1; d++) {
for (j = 0; j < number_of_elements - d - 1; j++) {
if (strlen(str[j]) < strlen(str[j + 1])) {
strcpy(temp, str[j]);
strcpy(str[j], str[j + 1]);
strcpy(str[j + 1], temp);
}
}
}
}
int main() {
FILE *dat;
int number_of_elements;
char str[MAX][MAXLEN];
int i = 0;
dat = fopen("ulaz.txt", "r");
if (dat == NULL) {
printf("Error");
}
while (!feof(dat) && !ferror(dat)) {
if (fgets(str[i], 100, dat) != NULL)
i++;
}
number_of_elements = i;
fclose(dat);
sort(str, number_of_elements);
for (int d = 0; d < i; d++) {
printf("%s", str[d]);
}
return 0;
}
Thanks in advance !