C++ 如何比较两个C字符串指针?

C++ 如何比较两个C字符串指针?,c++,C++,我试图编写代码来检查两个C字符串变量是否相同,除非大小写不同。给定两个输入的图书,程序应返回1,如果不正确,则应返回0。我的代码打印不准确 #include <iostream> #include <cctype> #include <cstring> using namespace std; int Judge_Char(const char* str1,const char* str2){ char first[20],second[20];

我试图编写代码来检查两个C字符串变量是否相同,除非大小写不同。给定两个输入的
图书
,程序应返回1,如果
不正确
,则应返回0。我的代码打印不准确

#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
int Judge_Char(const char* str1,const char* str2){
    char first[20],second[20];
    for(int i=0;i<20;i++){
        first[i]=str1[i];
        second[i]=str2[i]; //assigned pointers to variable
    }                      //bc I didn't know other ways to compare

    int k=0,l=0;
    for(k=0;first[k]!='\0';k++);
    for(l=0;second[l]!='\0';l++); //got the length of the chars here


    for(int i=0;i<k;i++){
        first[i]=toupper(first[i]);
    }                             //i converted them to same case here
    for(int i=0;i<l;i++){
        second[i]=toupper(second[i]);
    }

    for(int n=0;n<k;n++){
        for(int m=0;m<l;m++){
            if(first[n]==second[m]){
                return 1;           // i check whether they are same or not
            }
            else{
                return 0;
            }
        }
    }
}


int main()
{
    char a[20],b[20];
    cin>>a>>b;
    int flag=Judge_Char(a,b);
    cout<<flag<<endl;
    return 0;
}
#包括
#包括
#包括
使用名称空间std;
int Judge_Char(常量字符*str1,常量字符*str2){
第一个字符[20],第二个字符[20];

对于(inti=0;iC++,有两件事让我很恼火:

  • pi没有标准定义(但我们有一个非常复杂的随机数库,其中一些库需要定义pi的值才能实现)

  • <> LI> < P>在不区分大小写的基础上没有比较两个字符串的标准方法。一般情况下的比较问题的子集是由<代码> ISORLATE/<代码>、<代码> ISORL> <代码>、<代码> ToupP< C++ > >和 ToWoW。可以用更简单的术语来理解

    在Windows中使用
    ::_stricmp
    #include

    在Unix中,您使用
    strcasecmp
    #include


    这是一个有用的例子,即使它不支持德国党卫军、挪威人的斜杠o&c&c

    struct iLT
    {
        bool operator()(const std::string& lhs, const std::string& rhs) const {
            return strcasecmp(lhs.c_str(), rhs.c_str()) < 0;
        }
    };
    
    typedef std::map<std::string, double, iLT> MapWithCaseInsensitiveKeys;
    
    struct-iLT
    {
    布尔运算符()(常量std::string&lhs,常量std::string&rhs)常量{
    返回strcasecmp(lhs.c_str(),rhs.c_str())<0;
    }
    };
    typedef std::带有不区分大小写键的地图;
    
    <代码> >你知道如何正常地比较C字符串吗?请称为C字符串。<代码> cStult/Cuth>是不相关的C++类。不应该比较指针。应该比较指针指向的数据。@ ToMasMatthWest:这就是我试图做的。<代码> ToupP和 ToWale实际上不足够。(或实际上有用的)用于实现正确的区分大小写的比较。@康拉德鲁道夫:继续下去,漏网之鱼。C++ 20添加了C++,基本上不可能在Unicode上正确地执行<代码> ToupP< /Code >和<代码> ToWale
    。@ Bathsheba函数不是双射的。例如,代码>==0
    ,这是一个不能用大小写转换来表示的事实。