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 小时前
HTML5 浏览器支持
开发语言
wasp5202 小时前
AgentScope Java 核心架构深度解析
java·开发语言·人工智能·架构·agentscope
WHOVENLY2 小时前
【javaScript】- 笔试题合集(长期更新,建议收藏,目前已更新至31题)
开发语言·前端·javascript
慌糖2 小时前
流-为序列化解释
开发语言
LXS_3573 小时前
Day 18 C++提高 之 STL常用容器(string、vector、deque)
开发语言·c++·笔记·学习方法·改行学it
王琦03183 小时前
Python 函数详解
开发语言·python
胡伯来了4 小时前
13. Python打包工具- setuptools
开发语言·python
小鸡吃米…4 小时前
Python 中的多层继承
开发语言·python
deng-c-f4 小时前
Linux C/C++ 学习日记(53):原子操作(二):实现shared_ptr
开发语言·c++·学习
wanghowie4 小时前
01.07 Java基础篇|函数式编程与语言新特性总览
java·开发语言·面试