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

字符串数组

时间:2015-08-07 20:13:51      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:字符串数组

//有3个国家名,找出按字母顺序排在最前面的国家字符串

代码如下:

#include <iostream>
#include <string>
using namespace std;
int main()
{
    void smallest_string(char str[ ][30],int i);//函数声明
    int i;
    char country_name[3][30];//定义二维字符数组,把一个二维字符数组看成3个一维字符数组,它们各有30个元素
    for(i=0;i<3;i++)
    cin>>country_name[i];
    smallest_string(country_name,3);    //调用smallest_name函数,将字符数组名传递给形参
    return 0;
}
void smallest_string(char str[ ][30],int n)
{
    int i;
    char string[30];
    strcpy(string,str[0]);   //打擂台的算法
    for(i=0;i<n;i++)
        if(strcmp(str[i],string)<0)
        strcpy(string,str[i]);
    cout<<endl<<"the smallest string is: "<<string<<endl;
    }

技术分享

说明:只要更改此函数的部分参数,就能在任何情况下对字符进行比较。

版权声明:本文是我原创文章,大家可以随意转载哦!!

字符串数组

标签:字符串数组

原文地址:http://blog.csdn.net/jrz1900/article/details/47340703

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