「要成為絕世高手,並非一朝一夕,除非是天生武學奇才,但是這種人…萬中無一」
作者
閆小林
白天搬磚,晚上做夢。我有故事,你有酒麼?
例81:C語言實現用指向指針的指針的方法對5個字符串排序並輸出。#include<stdio.h>//頭文件
#include<string.h>
#define LINEMAX 20 //定義字符串的最大長度
int main()
{
void sort(char **point);//函數聲明
int i;//定義整型變量
char **point,*pstr[5],str[5][LINEMAX];//定義變量
for(i=0;i<5;i++)
{
pstr[i]=str[i]; //將第i個字符串的首地址賦予指針數組pstr的第i個元素
}
printf("輸入五個字符串:\n");//提示語句
for(i=0;i<5;i++)
{
scanf("%s",pstr[i]);
}
point=pstr;
sort(point);//調用sort函數
printf("————————————\n");//提示語句
printf("輸出排序後的結果:\n");//提示語句
for(i=0;i<5;i++)
{
printf("%s\n",pstr[i]);
}
return 0;//主函數返回值為0
}
void sort(char **point)//冒泡排序算法實現
{
int i,j;//定義整型變量
char *temp;//定義字符指針變量
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(strcmp(*(point+i),*(point+j))>0)//比較後交換字符串地址
{
temp=*(point+i);
*(point+i)=*(point+j);
*(point+j)=temp;
}
}
}
}
輸入五個字符串:
China
American
Japan
Back
Different
————————————
輸出排序後的結果:
American
Back
China
Different
Japan
--
Process exited after 2.574 seconds with return value 0
請按任意鍵繼續. . .