题解 | #字符串排序#
字符串排序
https://www.nowcoder.com/practice/dfeed0e0e4624814b122265e859783b2
#include<stdio.h> #include <string.h> void swap(char* str1, char* str2, size_t size) { char temp[size]; strcpy(temp, str1); strcpy(str1, str2); strcpy(str2, temp); } int main() { int n; char b[] = "stop"; while (scanf("%d", &n) != EOF) { char a[n][101]; int i, k; getchar(); for (i = 0; i < n; i++) { gets(a[i]); if (strcmp(b, a[i]) == 0) { break; } } for ( k = 0; k < i; k++) { for (int j = k + 1; j < i; j++) { char temp[101]; int length1 = strlen(a[k]), length2 = strlen(a[j]); if (length1 > length2) { swap(a[k], a[j], 101); } } } for (int t = 0; t < k; t++) { printf("%s\n", a[t]); } } }