1

I am trying to write code to check whether two C-string variables are the same barring the difference of capitalization. Given the two input books BOOKS, the program should return 1, and with Incorrect correct, it should return 0. My code doesn't print it accurately.

#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;
}
4
  • Do you know how to normally compare c-strings? Commented Jun 4, 2020 at 15:20
  • 3
    Please call them c-strings. CString is an unrelated C++ class. learn.microsoft.com/en-us/cpp/atl-mfc-shared/… Commented Jun 4, 2020 at 15:22
  • You should not compare pointers. You should compare the data that the pointers point to. Commented Jun 4, 2020 at 16:53
  • @ThomasMatthews That's what I tried to do. Commented Jun 4, 2020 at 16:56

1 Answer 1

3

Two things annoy me about C++:

  1. There is no standard definition for pi (yet we have an extremely sophisticated random number library some of which requires a value for pi to be defined for it to be implementable).

  2. There is no standard way of comparing two strings on a case-insensitive basis. A subset of the general case compare problem is defined by isupper, islower, toupper, and tolower. Yes indeed a full locale-aware case-insensitive compare is beyond the scope of the C++ standard library, but one could be conceived in simpler terms.

In Windows you use ::_stricmp and #include <string.h>

In Unix you use strcasecmp and #include <strcasecmp>


An example where it's useful, even if it doesn't support the German SS, Norwegian Slashed 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;
Sign up to request clarification or add additional context in comments.

10 Comments

toupper and tolower are not actually sufficient (or indeed useful) for implementing a correct case insensitive comparison.
@KonradRudolph: Go on, spill the beans?
C++20 adds std::pi_v
C++ is fundamentally incapable of doing toupper and tolower properly on Unicode.
@Bathsheba The functions are not bijective. For instance, correct_stricmp("straße", "STRASSE") == 0, a fact which can’t be expressed in terms of case conversion.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.