指针数组的应用

来源:互联网 发布:redis和mysql的结合 编辑:程序博客网 时间:2024/05/29 10:22

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