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

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

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

环境:Linux Ubuntu(云服务器)
工具:vim

 

代码块
/*************************************************************************
	> 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;
}
运行结果显示如下

在这里插入图片描述

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-01 20:00:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-01 20:00:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-02-01 20:00:02       82 阅读
  4. Python语言-面向对象

    2024-02-01 20:00:02       91 阅读

热门阅读

  1. shell学习

    2024-02-01 20:00:02       52 阅读
  2. Ubuntu18搭建Kubernetes集群

    2024-02-01 20:00:02       60 阅读
  3. 踩坑日记:opencv多次绘图绘图不全

    2024-02-01 20:00:02       53 阅读
  4. Python中类的成员方法

    2024-02-01 20:00:02       50 阅读
  5. jQuery html的使用

    2024-02-01 20:00:02       44 阅读
  6. R语言入门笔记2.1

    2024-02-01 20:00:02       46 阅读
  7. 24校招,小鹏汽车自动驾驶测试工程师二面

    2024-02-01 20:00:02       43 阅读
  8. MySQL InnoDB是如何实现ACID的?

    2024-02-01 20:00:02       51 阅读