发布网友 发布时间:2024-12-22 14:49
共1个回答
热心网友 时间:6分钟前
#include <stdio.h>
#include<string.h>
#define N 7
void sort(char *language[]);
void output(char *language[]);
void main()
{
char *language[N]={"PASCAL","BASIC","C/C++","Fortran","Turbo C","JAVA","Html"};
output(language);
sort(language);
output(language);
}
void sort(char *language[])
{
int i,j;
char *t;
for(i=0;i<N-1;i++)
for(j=0;j<N-1-i;j++)
if(strcmp(language[j],language[j+1])>0)
{
t=language[j]; //不能使用strcpy函数,因为language定义的是指针数组,只能存放地址,
//对language进行地址的交换也能达到排序目的
language[j]=language[j+1];
language[j+1]=t;
}
}
void output(char *language[])
{
int i;
for(i=0;i<N;i++)
printf("%s ",language[i]);
printf("\n");
}