码迷,mamicode.com
首页 > 编程语言 > 详细

指针数组的应用

时间:2015-06-11 19:22:31      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

问题描述
在主函数中输入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;
    }
}

指针数组的应用

标签:

原文地址:http://blog.csdn.net/a819721810/article/details/46460931

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!