0
votes
#include <strings.h>
#include <iostream>
#include <string>
#include <cstring>

using namespace std;

int main()
{
    string s = "test";
    if(strcasecmp(s, "TEST"))
        cout << "equals"<< endl;

    return 0;
}

Help needed on how to use strcasecmp, when i tried to compile, the compiler keep shows an error message

error: cannot convert ‘std::string {aka std::basic_string}’ to ‘const char*’ for argument ‘1’ to ‘int strcasecmp(const char*, const char*)

4
try with strcasecmp(s.c_str(), "TEST") - max66

4 Answers

3
votes

The strcasecmp() function expects a pointer to a null-terminated char string, you cannot pass a std::string directly to it.

The correct way to use this should be:

strcasecmp(s.c_str(), "TEST")

Moreover, the code looks incorrect in your program, because strcasecmp() returns a ZERO in case the strings are equal. So, you should be using the following:

if(strcasecmp(s, "TEST") == 0)
    cout << "equals"<< endl;
4
votes

strcasecmp takes a const char*, not a std::string as argument. Use strcasecmp(s.c_str(), "TEST") instead, or have a s be a C-style string in the first place.

3
votes

It means strcasecmp expect a C style string (0 terminated char[]) not a C++ string. You can get one with s.c_str().

1
votes

you can try

strcasecmp(s.c_str(), "TEST")