用指向指针的指针的方法对5个字符串排序并输出 C语言

2016-06-20 06:30:22  分类: c程序设计第四版谭浩强课后答案  参与:

 用指向指针的指针的方法对5个字符串排序并输出

谭浩强c语言程序设计第四版答案
以下是此题的【c源代码】,需要【c++源代码】请点击进入

#define LINEMAX 20              /*定义字符串的最大长度*/
int main()
{int i;
 char **p,*pstr[5],str[5][LINEMAX];
 for (i=0;i<5;i++)
   pstr[i]=str[i];   /*将第i个字符串的首地址赋予指针数组 pstr 的第i个元素*/
 printf("input 5 strings:\n");
 for (i=0;i<5;i++)
    scanf("%s",pstr[i]);
 p=pstr;
 sort(p);
 printf("strings sorted:\n");
 for (i=0;i<5;i++)
    printf("%s\n",pstr[i]);
}

sort(char **p)            /*冒泡法对5个字符串排序函数*/
{int i,j;
 char *temp;
 for (i=0;i<5;i++)
  {for (j=i+1;j<5;j++)
    {if (strcmp(*(p+i),*(p+j))>0)      /*比较后交换字符串地址*/
      {temp=*(p+i);
       *(p+i)=*(p+j);
       *(p+j)=temp;
      }
     }
  }
 return 0;
}

 

来源:c程序设计第四版谭浩强课后答案

本文链接:http://www.wb98.com/c/post/tanhaoqiang_8.20.html

本站文章搜索:

<< 上一篇下一篇 >>

搜索

网站分类

Tags列表

赞助商链接