String Sorting * * * *
/*
===============================================================
Sort any string in ascending order!
===============================================================
*/
#include <stdio.h>
#include <string.h>
#define N 3//control Sort Quantity
#define M 100//control word length
sorting function functions
void PX (char s[n][m])
{
int i,j;
Char Temp[m];
for (i=0;i<n-1;i++)
for (j=i+1;j<n;j++)
if (strcmp (S[i],s[j]) ==1)
{
strcpy (Temp,s[i]);
strcpy (S[i],s[j]);
strcpy (s[j],temp);
}
}
Main function
void Main ()
{
int i;
Char P[n][m];
printf ("input%d words: \ n", "N.");
for (i=0;i<n;i++)
Gets (P[i]);
PX (P);
printf ("After sorting: \ n");
for (i=0;i<n;i++)
Puts (P[i]);
}
/*
===============================================================
Evaluation:
First think of the use of functions, at least to write the main function;
Define the function px to implement the sorting function, which used the strcmp and strcpy and arrays, and then
Write the main function, simply call the PX function to complete. Subsequent learning of pointers can also continue to rewrite the program.
On circular control,
Two for loops inside the function body! Bubble Sort method is used here!
===============================================================
*/
Copyright Notice: Bo Main article can be reproduced non-commercial, but please be sure to indicate the source, because the level is limited, inevitably error, in this disclaimer.
C language Function call 18-multi-string ordering