题解 | #字符串排序#
字符串排序
https://www.nowcoder.com/practice/5af18ba2eb45443aa91a11e848aa6723
#include <stdio.h> #include <string.h> #include <stdlib.h> // 区分大小写的方法,大写排在小写前面 int main() { int n; int i; int j; char* c; char* str[1000]; scanf("%d", &n); for (i = 0; i < n; i++) { str[i] = (char*)malloc(sizeof(char) * 100); scanf("%s", str[i]); } for (i = 0; i < n - 1; i++) { for (j = 0; j < n - 1 - i; j++) { if (strcmp(str[j], str[j + 1]) > 0) { c = str[j]; str[j] = str[j + 1]; str[j + 1] = c; } } } for (i = 0; i < n; i++) { printf("%s\n", str[i]); } for (i = 0; i < n; i++) { free(str[i]); } return 0; }