将字符串用指针排序

来源:百度知道 编辑:UC知道 时间:2024/06/14 15:35:15
rheyh

const int len=5;
char *str[len]={"Abort","W32","Exit","Love","Thank"};
for(int i=0; i<len-1; i++)
for(int j=i+1; j<len; j++)
{
if(strcmp(str[i],str[j])>0)
{
char *tmp=str[i];
str[i]=str[j];
str[j]=tmp;
}
}

for(i=0; i<len; i++)
cout<<str[i]<<endl;

串:
char buf[20]="x23yabuy";
char *p=buf;
for(int i=0; i<strlen(buf)-1; i++)
for(int j=i+1; j<strlen(buf); j++)
{
if(buf[i]>buf[j])
{
char temp=*(p+i);
*(p+i)=*(p+j);
*(p+j)=temp;

}
}
cout<<buf<<endl;