注册 登录
编程论坛 C++教室

输入两个字符串比较大小

psyqin 发布于 2018-05-24 21:59, 1590 次点击
输入两个字符串,比较它们的大小。我的做法是这样的,但程序显示不完全正确,不知错误在哪里,请指点,多谢!
#include<bits/stdc++.h>
#include<cstring>
using namespace std;
int main()
{
    char a[1000],b[1000];
    cin>>a>>b;
    if(strlen(a)>strlen(b))   
        cout<<b;
    else                       
        cout<<a;

    return 0;
}
5 回复
#2
Jonny02012018-05-24 23:42
你都
#include<bits/stdc++.h>

居然不用 STL
程序代码:
#include <iostream>

using namespace std;
int main(int argc, char *argv[]) {
    string a, b;
    cin >> a >> b;
    cout << (a > b ? b : a) << endl;
}

还有, 你是输出小一点的那一个字串, 如果不是的话就可能反了

[此贴子已经被作者于2018-5-24 23:51编辑过]

#3
rjsp2018-05-25 08:19
回复 2楼 Jonny0201
全用库函数
程序代码:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main( void )
{
    string a, b;
    cin >> a >> b;
    cout << min(a,b) << endl;
}

#4
yexinxin2018-05-28 13:35
#include <stream>
#include <string>
using namespace std;
int main()
{
    string a,b;
    cout<<"请输入两个字符串:"<<endl;
    cin>>a>>b;
    if(a.size()>b.size())
        cout<<a<<end;
    else
        cout<<b<<endl;
    else
        cout<<"一样长"<<endl;
    system("pause");
    return 0;
}
#5
yexinxin2018-05-28 13:36
#include <stream>
#include <string>
using namespace std;
int main()
{
    string a,b;
    cout<<"请输入两个字符串:"<<endl;
    cin>>a>>b;
    if(a.size()>b.size())
        cout<<a<<end;
    else
        cout<<b<<endl;
    else
        cout<<"一样长"<<endl;
    system("pause");
    return 0;
}
1