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;
}
运行结果显示如下
相关推荐
沐怡旸2 小时前
【底层机制】std::string 解决的痛点?是什么?怎么实现的?怎么正确用?
c++·面试
River4165 小时前
Javer 学 c++(十三):引用篇
c++·后端
感哥8 小时前
C++ std::set
c++
侃侃_天下8 小时前
最终的信号类
开发语言·c++·算法
博笙困了8 小时前
AcWing学习——差分
c++·算法
echoarts8 小时前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Aomnitrix9 小时前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
青草地溪水旁9 小时前
设计模式(C++)详解—抽象工厂模式 (Abstract Factory)(2)
c++·设计模式·抽象工厂模式
青草地溪水旁9 小时前
设计模式(C++)详解—抽象工厂模式 (Abstract Factory)(1)
c++·设计模式·抽象工厂模式
感哥9 小时前
C++ std::vector
c++