BUPT-homework/semester1/pset13/2-3-ArrayEX.c

45 lines
722 B
C
Raw Normal View History

2021-12-10 23:31:54 +08:00
#include <stdio.h>
#include <string.h>
#define MAX 100001
void sort(char **arr, int n);
int main(void) {
2021-12-10 23:51:00 +08:00
char str[MAX];
2021-12-10 23:31:54 +08:00
int n;
scanf("%d", &n);
2021-12-10 23:51:00 +08:00
char *arr[n];
2021-12-10 23:31:54 +08:00
getchar();
2021-12-10 23:51:00 +08:00
// Get input
char ch;
int loc = 0;
2021-12-10 23:31:54 +08:00
for (int i = 0; i < n; i++) {
2021-12-10 23:51:00 +08:00
arr[i] = &str[loc];
while ((ch = getc(stdin)) != '\n') {
str[loc++] = ch;
}
str[loc++] = '\0';
2021-12-10 23:31:54 +08:00
}
2021-12-10 23:51:00 +08:00
sort(arr, n);
2021-12-10 23:31:54 +08:00
for (int i = 0; i < n; i++) {
2021-12-10 23:51:00 +08:00
printf("%s\n", arr[i]);
2021-12-10 23:31:54 +08:00
}
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;
}
}
}
}