First type: (similar to bubble sort) #include <stdio.h> #include <stdlib.h> #include <string.h>int main () {char a[5][30]={" AAA "," BBB "," CCC "," adb "," DEGF "};int i=0;int j=0;char b[30];for (i=0;i<4;i++) {for (j=0;j<4-i;j++) {if (strcmp (a[j ],a[j+1] >0) {strcpy (b,a[j]); strcpy (a[j],a[j+1)); strcpy (a[j+1],b);}} for (i=0;i<5;i++) {printf ("%s\n", A[i]);} System ("pause"); return 0;} Second: fast-#include<stdio.h> #include <stdlib.h> #include <string.h>int my_strcmp (const void* P,const void* q) {/*char* a= (char*) p; char* b= (char*) q;*/if (strcmp ((char*) p, (char *) q) ==0) {return 0;} else if (strcmp (char*) p, (char *) q) >0) return 1;elsereturn-1; int main () {char a[5][30]={"AAA", "BBB", "Bdv", "FGH", "DDS"};int I;qsort (A,sizeof (a)/sizeof (a[0]), sizeof (A[0]), My_ strcmp); for (i=0;i<5;i++) {printf ("%s\n", A[i]);} System ("pause"); return 0;}
This article from "Liveyoung" blog, reproduced please contact the author!
Creates an array of strings, a total of 5 elements, each of which holds up to 30 characters, and writes a function to sort the entire array.