xiaofengshu0614 2024-11-27 17:56 采纳率: 100%
浏览 43
已结题

如何实现c++字符串对比

1:两个字符串长度不等。比如 Beijing 和 Hebei
2:两个字符串不仅长度相等,而且相应位置上的字符完全一致(区分大小写),比如 Beijing 和 Beijing
3:两个字符串长度相等,相应位置上的字符仅在不区分大小写的前提下才能达到完全一致(也就是说,它并不满足情况2)。比如 beijing 和 BEIjing
4:两个字符串长度相等,但是即使是不区分大小写也不能使这两个字符串一致。比如 Beijing 和 Nanjing
如何区分这4种情况?

  • 写回答

1条回答 默认 最新

  • 猿享天开 优质创作者: 编程框架技术领域 2024-11-27 18:04
    关注

    要区分这四种情况,我们可以使用C++中的std::string来实现字符串比较。具体步骤如下:

    1、检查字符串长度:首先比较两个字符串的长度。
    2、区分大小写比较:如果长度相等,使用==运算符进行区分大小写的比较。
    3、不区分大小写比较:如果长度相等且区分大小写比较不一致,则进行不区分大小写的比较。
    下面是一个C++程序的实现:

    #include <iostream>   
    #include <string>     
    #include <algorithm> 
    
    // 定义一个枚举类型,用于表示字符串比较的结果
    enum StringComparisonResult {
        DifferentLength,        // 两个字符串长度不同
        ExactMatch,             // 两个字符串完全相同
        CaseInsensitiveMatch,   // 两个字符串在忽略大小写的情况下相同
        NoMatch                 // 两个字符串长度相等,但内容不同
    };
    
    // 函数compareStrings用于比较两个字符串,并返回比较结果
    StringComparisonResult compareStrings(const std::string& str1, const std::string& str2) {
        // 1. 检查字符串长度是否不同
        if (str1.length() != str2.length()) {
            return DifferentLength; // 如果长度不同,返回DifferentLength
        }
    
        // 2. 检查字符串是否完全相同(区分大小写)
        if (str1 == str2) {
            return ExactMatch; // 如果完全相同,返回ExactMatch
        }
    
        // 3. 将两个字符串转换为小写以进行不区分大小写的比较
        std::string str1Lower = str1;
        std::string str2Lower = str2;
    
        // 使用std::transform将字符串中的每个字符转换为小写
        std::transform(str1Lower.begin(), str1Lower.end(), str1Lower.begin(), ::tolower);
        std::transform(str2Lower.begin(), str2Lower.end(), str2Lower.begin(), ::tolower);
    
        // 如果转换后字符串相同,返回CaseInsensitiveMatch
        if (str1Lower == str2Lower) {
            return CaseInsensitiveMatch;
        }
    
        // 4. 如果不匹配,返回NoMatch
        return NoMatch;
    }
    
    int main() {
        std::string str1, str2; // 定义两个字符串变量用于存储用户输入
        std::cout << "Enter the first string: ";
        std::getline(std::cin, str1); // 获取用户输入的第一个字符串
        std::cout << "Enter the second string: ";
        std::getline(std::cin, str2); // 获取用户输入的第二个字符串
    
        // 调用compareStrings函数比较两个字符串,并存储结果
        StringComparisonResult result = compareStrings(str1, str2);
    
        // 根据比较结果输出相应的消息
        switch (result) {
            case DifferentLength:
                std::cout << "The strings have different lengths." << std::endl;
                break;
            case ExactMatch:
                std::cout << "The strings are an exact match." << std::endl;
                break;
            case CaseInsensitiveMatch:
                std::cout << "The strings are a match when case is ignored." << std::endl;
                break;
            case NoMatch:
                std::cout << "The strings match in length but not in content." << std::endl;
                break;
        }
    
        return 0; // 结束程序
    }
    
    
    
    

    程序说明:
    字符串长度检查:首先,我们比较两个字符串的长度。如果不同,则返回DifferentLength。
    区分大小写比较:如果长度相同,直接比较两个字符串。如果相等,则返回ExactMatch。
    不区分大小写比较:如果区分大小写比较不相等,将两个字符串转换为小写后再比较。如果相等,则返回CaseInsensitiveMatch。
    最终不匹配:如果上述比较都不符合,则返回NoMatch。
    该程序充分利用了std::transform来转换字符串为小写以便进行不区分大小写的比较。这样可以有效地判断四种情况。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月9日
  • 已采纳回答 12月1日
  • 创建了问题 11月27日