//有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;
}
说明:只要更改此函数的部分参数,就能在任何情况下对字符进行比较。