字符串排序

| December 11, 2007 19:24 | timmy | Via Original
输入5个字符串,按从大到小排序后输出,要求用指针数组实现。
例:(括号内为说明)
输入
food
appear
zoo
911
apple
输出
zoo
food
apple
appear
911


#include <stdio.h>
#include <string.h>
void main()
{
  int i,j;    
  char s[5][80],*p[5],temp[80];
  for(i=0;i<5;i++){
    scanf("%s",s[i]);
    p[i]=s[i];
  }
  for(i=1;i<5;i++)
    for(j=0;j<5-i;j++)
      if(strcmp(s[j],s[j+1])<0){
        strcpy(temp,s[j]);
        strcpy(s[j],s[j+1]);
        strcpy(s[j+1],temp);
      }  
  for(i=0;i<5;i++)
    puts(p[i]);
}
Tags:
Program/Code » C/C++ | Comments(0) | Trackbacks(0) | Reads(159)
Add a comment
 Site URI
 Email
  Password Optional
 Nickname  *  [Register]
               

 
Emots
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
Enable HTML
Enable UBB
Enable Emots
Hidden
Remember