指针数组的应用

问题描写叙述
在主函数中输入10个长度不超过10的字符串,用还有一个函数对它们排序,然后在主函数输出这10个已排好序的字符串。

要求用指针数组来处理

#include<stdio.h>
#include<string>
#include<iostream>
using namespace std;
void sort(char *p[],int n){

    int i,j,k;
    char temp[10];
    for(i=0;i<n-1;i++){
        k=i;
        for (j = i+1; j <n; j++)

            if (strcmp(p[j],p[k])<0)
            {
                k=j;
            }
            if (k!=i)
            {
                strcpy(temp,p[i]);
                strcpy(p[i],p[k]);
                strcpy(p[k],temp);
            }

}
}
void main(){

    int i;
    char *p[10],str[10][10];
    cout<<"inputs 10 strings:"<<endl;
    for ( i = 0; i <10; i++)
    {
        cin>>str[i];
        p[i]=str[i];
    }
    sort(p,10);
    cout<<"Now,the sequence is:\n";
    for (i = 0; i < 10; i++)
    {
        cout<<p[i]<<endl;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值