There are 2 types of string sorting:
1 length strlen
2 comparison strcmp
Read a 3-line array of two-dimensional strings, using the string length function strlen, to sort from large to small, using bubbling sorting.
1#include <stdio.h>2#include <string.h>3 Main ()4 {5 intI, J;6 Chart[ -], a[3][ -];7 for(i =0; I <3; i++)/*assign a value to a table*/8 {9 gets (A[i]);Ten } One Aprintf"\ n"); - for(i =0; I <3; i++)/*Output a table*/ - { the puts (a[i]); - } -printf"\ n"); - + for(i =0; I <3-1; i++) - { + for(j = i +1; J <3; j + +) A { at if(Strlen (A[i]) < strlen (A[j]))/*To find the string length function strlen*/ - { -strcpy (t, A[i]);/*string copy function strcpy*/ - strcpy (A[i], a[j]); - strcpy (A[j], t); - } in } - } to + for(i =0; I <3; i++)/*Output a table*/ - { the puts (a[i]); * } $}
Read a 3-line array of two-dimensional strings, using the string comparison function strcmp, to sort from large to small, using bubbling sorting.
1#include <stdio.h>2#include <string.h>3 Main ()4 {5 intI, J;6 Chart[ -], a[3][ -];7 for(i =0; I <3; i++)/*assign a value to a table*/8 {9 gets (A[i]);Ten } One Aprintf"\ n"); - for(i =0; I <3; i++)/*Output a table*/ - { the puts (a[i]); - } -printf"\ n"); - + for(i =0; I <3-1; i++) - { + for(j = i +1; J <3; j + +) A { at if(strcmp (A[i], a[j]) <0)/*string comparison function strcmp, if the former a[i]< the latter a[j], the function value is less than 0*/ - { -strcpy (t, A[i]);/*string copy function strcpy*/ - strcpy (A[i], a[j]); - strcpy (A[j], t); - } in } - } to + for(i =0; I <3; i++)/*Output a table*/ - { the puts (a[i]); * } $}
123
The National Computer Grade examination two level course-C language Programming _ the 10th Chapter _ String