69 date:2021.3.3
要点:
冒泡排序
for(i = 0; i<N; i++)
for(j =i+1; j<N; j++)
{
if(s[i]>s[j])
{
t =s[i];
s[i] = s[j];
s[j] = t;
}
}
strcmp()
详细代码如下:
#include <stdio.h>
#include <string.h>
#define MAXLINE 20
void fun ( char *pstr[6])
{ int i, j ;
char *p ;
for (i = 0 ; i < 5 ; i++ ) {
/**************found**************/
for (j = i + 1; j < 6; j++)
{
if(strcmp(*(pstr + i), *(pstr + j)) > 0)
{
p = *(pstr + i) ;
/**************found**************/
*(pstr + i) = *(pstr + j) ;
*(pstr + j) = p ;
}
}
}
}
void main( )
{ int i ;
char *pstr[6], str[6][MAXLINE] ;
for(i = 0; i < 6 ; i++) pstr[i] = str[i] ;
printf( "\nEnter 6 string(1 string at each line): \n" ) ;
for(i = 0 ; i < 6 ; i++) scanf("%s", pstr[i]) ;
fun(pstr) ;
printf("The strings after sorting:\n") ;
for(i = 0 ; i < 6 ; i++) printf("%s\n", pstr[i]) ;
}