Comparing strings

Eliza 21 Reputation points
2022-08-29T07:15:24.14+00:00

Im trying to create some snippets to compare strings, following the docs
https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/strncmp-wcsncmp-mbsncmp-mbsncmp-l?view=msvc-170

I build these:

bool iequal(const std::string &first, const std::string &second, bool casesensitive = false)  
{  
    if (casesensitive)  
    {  
        if (strncmp(first.data(), second.data(), second.length() == 0))  
            return true;  
        else  
            return false;  
    }  
  
    if (_strnicmp(first.data(), second.data(), second.length() == 0))  
        return true;  
    else  
        return false;  
}  
  
bool iequal(const std::wstring &first, const std::wstring &second, bool casesensitive = false)  
{  
    if (casesensitive)  
    {  
        if (wcsncmp(first.c_str(), second.c_str(), second.length() == 0))  
            return true;  
        else  
            return false;  
    }  
  
    if (_wcsnicmp(first.c_str(), second.c_str(), second.length() == 0))  
        return true;  
    else  
        return false;  
}  

Is it ok to use second.length() or i should use wcslen / strlen to get the string length?

== 0 meant the strings matches right?

I tried:

if (iequal(L"green", L"green"))  
{  
    bool match = true;  
}  
  
  
if (iequal(L"red", L"red"))  
{  
    bool match = true;  
}  

But in none of them the code gets inside of the if, what im doing wrong?

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,482 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Barry Schwarz 1,751 Reputation points
    2022-08-29T08:40:49.97+00:00

    You have misplaced your parentheses.

    The statement you coded
    if (strncmp(first.data(), second.data(), second.length() == 0))
    has the arguments to strncmp parsed as
    arg 1 = first.data()
    arg 2 = second.data()
    arg 3 = second.length() == 0
    Since second.length() is not zero, this argument is evaluated to false before calling strncmp. Since there is no other text inside the if statement, the statement is process as
    if ( strncmp(...) )
    and since the first two arguments are equal strncmp returns 0 which the if streats as false.
    The if statement should read
    if (strncmp(first.data(), second.data(), second.length()) == 0)
    so that arg3 becomes second.length() and the value returned by strncmp is then compared to 0.

    The same problem exists in the _wcsnicmp if statement.

    0 comments No comments

  2. RLWA32 39,451 Reputation points
    2022-08-29T08:52:08.763+00:00

    There are misplaced parenthesis in the posted code in the various "if" statements.

    For example, compare posted code -

         if (strncmp(first.data(), second.data(), second.length() == 0))  
    

    corrected version

         if (strncmp(first.data(), second.data(), second.length()) == 0)  
      
    
    0 comments No comments