c程序设计第四版谭浩强
用指针数组处理上一题目(在主函数输入10个等长的字符串。用另一函数对它们排序。然后在主函数输出这10个已经排好序的字符串。),字符串不等长。
c程序设计(第四版)学习辅导 谭浩强 编著
以下是此题的【c源代码】,需要【c++源代码】请点击进入
#include <stdio.h>
#include <string.h>
int main()
{void sort(char *[]);
int i;
char *p[10],str[10][20];
for (i=0;i<10;i++)
p[i]=str[i];
printf("input 10 strings:\n");
for (i=0;i<10;i++)
scanf("%s",p[i]);
sort(p);
printf("Now,the sequence is:\n");
for (i=0;i<10;i++)
printf("%s\n",p[i]);
return 0;
}
void sort(char *s[])
{int i,j;
char *temp;
for (i=0;i<9;i++)
for (j=0;j<9-i;j++)
if (strcmp(*(s+j),*(s+j+1))>0)
{temp=*(s+j);
*(s+j)=*(s+j+1);
*(s+j+1)=temp;
}
}
本文链接:http://www.wb98.com/c/post/tanhaoqiang_8.12.html
本站文章搜索: