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;
}
运行结果显示如下
相关推荐
whoarethenext32 分钟前
C++ OpenCV 学习路线图
c++·opencv·学习
闻缺陷则喜何志丹42 分钟前
【强连通分量 缩点 拓扑排序】P3387 【模板】缩点|普及+
c++·算法·拓扑排序·洛谷·强连通分量·缩点
钮钴禄·爱因斯晨43 分钟前
Java 面向对象进阶之多态:从概念到实践的深度解析
java·开发语言·数据结构
鸽子炖汤43 分钟前
Java中==和equals的区别
java·开发语言·jvm
有个傻瓜1 小时前
PHP语言核心技术全景解析
开发语言·kubernetes·php
菥菥爱嘻嘻1 小时前
JS手写代码篇---手写ajax
开发语言·javascript·ajax
hutaotaotao1 小时前
c++中的输入输出流(标准IO,文件IO,字符串IO)
c++·io·fstream·sstream·iostream
江梦寻1 小时前
MacOS下Homebrew国内镜像加速指南(2025最新国内镜像加速)
开发语言·后端·python·macos·架构·策略模式
AL流云。1 小时前
【优选算法】C++滑动窗口
数据结构·c++·算法
霖檬ing1 小时前
Python——MySQL远程控制
开发语言·python·mysql