當前位置:成語大全網 - 新華字典 - 壹道c語言程序題: 輸入5個字符串,按字典順序將其重新排列輸出。用字符串指針實現。

壹道c語言程序題: 輸入5個字符串,按字典順序將其重新排列輸出。用字符串指針實現。

#include<stdio.h>

#include<string.h>

int main()

{

void sort(char (*p)[20]);

char a[5][20];

int i;

for(i=0;i<5;i++)

gets(a[i]);

sort(a);

}

void sort(char (*p)[20])

{

char a[20];

int i,j;

for(i=0;i<5;i++)

for(j=0;j<5-i-1;j++)

{

if(strcmp(p[j],p[j+1])>0)

{

strcpy(a,p[j]);

strcpy(p[j],p[j+1]);

strcpy(p[j+1],a);

}

}

printf("after sorted:\n");

for(i=0;i<5;i++)

puts(p[i]);

printf("\n");

}