BUPT-homework/semester1/pset13/2-3-ArrayEX.c
2021-12-10 23:51:00 +08:00

45 lines
722 B
C

#include <stdio.h>
#include <string.h>
#define MAX 100001
void sort(char **arr, int n);
int main(void) {
char str[MAX];
int n;
scanf("%d", &n);
char *arr[n];
getchar();
// Get input
char ch;
int loc = 0;
for (int i = 0; i < n; i++) {
arr[i] = &str[loc];
while ((ch = getc(stdin)) != '\n') {
str[loc++] = ch;
}
str[loc++] = '\0';
}
sort(arr, n);
for (int i = 0; i < n; i++) {
printf("%s\n", arr[i]);
}
return 0;
}
void sort(char **arr, int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1; j++) {
if (strcmp(arr[j], arr[j + 1]) > 0) {
char *tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
}
}