在住函數中輸入10個等長的字符串,用用一個函數對它進行排序。(1.用scanf輸入10個字符串,2,用指針數組對它進行排序)
#include <stdio.h>
#include<string.h>
void print(char *[],int);
void print_split(int);
void sort(char *[],int);
int main(int argc,char *argv[])
{
int i;
char *arr[10],str[10][20],**p;
for (i = 0; i < 10; i++)
{
arr[i] = str[i];
}
p = arr;
printf("please input 10 string:\n");
for (i = 0;i < 10; i++)
{
scanf("%s",*p++);
}
p = arr;
print_split(10);
printf("the source is :\n");
print(arr,10);
print_split(10);
printf("the sort result:\n");
sort(arr,10);
print(arr,10);
return 0;
}
void print(char *arr[],int n)
{
char **p;
p = arr;
int i;
for (i = 0; i < n; i++)
{
printf("%s\n",*p++);
}
}
void sort(char *arr[],int n)
{
char *temp;
int i,j,k;
for (i = 0; i < n - 1; i++)
{
k = i;
for (j = i + 1; j < n; j++)
{
if (strcmp(arr[k],arr[j]) > 0)
{
k = j;
}
}
if(k != i)
{
temp = arr[i];
arr[i]= arr[k];
arr[k] = temp;
}
}
}
void print_split(int n)
{
while(n--)
{
printf("-");
}
printf("\n");
}