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;
}
运行结果显示如下
相关推荐
NiNi_suanfa2 小时前
【Qt】Qt 批量修改同类对象
开发语言·c++·qt
小糖学代码2 小时前
LLM系列:1.python入门:3.布尔型对象
linux·开发语言·python
Data_agent2 小时前
1688获得1688店铺详情API,python请求示例
开发语言·爬虫·python
信奥胡老师3 小时前
苹果电脑(mac系统)安装vscode与配置c++环境,并可以使用万能头文件全流程
c++·ide·vscode·macos·编辑器
妖灵翎幺3 小时前
C++ 中的 :: 操作符详解(一切情况)
开发语言·c++·ide
Halo_tjn3 小时前
虚拟机相关实验概述
java·开发语言·windows·计算机
star _chen3 小时前
C++实现完美洗牌算法
开发语言·c++·算法
周杰伦fans3 小时前
pycharm之gitignore设置
开发语言·python·pycharm
繁星星繁4 小时前
【C++】脚手架学习笔记 gflags与 gtest
c++·笔记·学习
别叫我->学废了->lol在线等4 小时前
演示 hasattr 和 ** 解包操作符
开发语言·前端·python