C++ //练习 3.39 编写一段程序,比较两个string对象。再编写一段程序,比较两个C风格字符串的内容。

C++ Primer(第5版) 练习 3.39

练习 3.39 编写一段程序,比较两个string对象。再编写一段程序,比较两个C风格字符串的内容。

环境:Linux Ubuntu(云服务器)
工具:vim
代码块
cpp 复制代码
/*************************************************************************
	> File Name: ex3.39.cpp
	> Author: 
	> Mail: 
	> Created Time: Thu 01 Feb 2024 02:41:54 PM CST
 ************************************************************************/

#include<iostream>
#include<cstring>
using namespace std;

int main(){
    string s1, s2;
    cout<<"Enter string1: ";
    getline(cin, s1);
    cout<<"Enter string2: ";
    getline(cin, s2);
    
    if(s1 == s2){
        cout<<"string1 is equal to string2."<<endl;
    }
    else if(s1 > s2){
        cout<<"string1 is greater than string2."<<endl;
    }
    else{
        cout<<"string1 is less than string2."<<endl;
    }

    char s3[80];
    char s4[80];
    
    cout<<"Enter s3: ";
    cin.getline(s3, sizeof(s3));
    fflush(stdin);
    cout<<"Enter s4: ";
    cin.getline(s4, sizeof(s4));

    if(strcmp(s3, s4) == 0){
        cout<<"s3 is equal to s4."<<endl;
    }
    else if(strcmp(s3, s4) > 0){
        cout<<"s3 is greater than s4."<<endl;
    }
    else{
        cout<<"s3 is less than s4."<<endl;
    }

    return 0;
}
运行结果显示如下
相关推荐
人道领域6 分钟前
【黑马点评日记】社交平台用户关注功能全解析Feed流相关操作
java·开发语言·数据库·redis·python
xiaoshuaishuai827 分钟前
C# DeepSeek V4 与 V3对比
开发语言·c#·量子计算
shehuiyuelaiyuehao36 分钟前
算法18,二分查找
java·开发语言·算法
IT策士39 分钟前
Python mcp研究:入门到精通
开发语言·python·qt
罗技12341 分钟前
告别“兼容模式“:Easysearch 有了自己的官方 Python 客户端
开发语言·python
weixin_4467291641 分钟前
java网络通讯
java·开发语言
IT策士43 分钟前
Python 常见的设计模型:入门到精通
开发语言·python
不会写DN1 小时前
如何通过 Python 实现招聘平台自动投递
开发语言·前端·python
lbb 小魔仙1 小时前
Ollama + Python 本地大模型部署与API调用:从零开始搭建私有AI助手
开发语言·人工智能·python
邪修king1 小时前
C++ typename & auto 彻底讲透:核心作用、推导规则、避坑指南
开发语言·c++