String similarity algorithm (c++) Parameters:

first - the first string.
second - the second string.
similarCharacters - outputs the number of similar characters.
tolerance - the number of mistakes allowed to consider the strings "similar."

Function:

bool Str_Similar(std::string first, std::string second, unsigned int* similarCharacters = nullptr, int tolerance = INT_MIN)
{
    // Don't even check if either strings are empty.
    if(first.empty() || second.empty()) return false;

    // Determine if the first is greater than or equal to the second.
    const bool firstGreaterOrEqualToSecond = first.length() &gt;= second.length();

    // By default, set the tolerance to half the length of the smaller string.
    if(tolerance == INT_MIN) tolerance = (firstGreaterOrEqualToSecond ? (second.length() / 2) : (first.length() / 2));
    if(tolerance &lt; 0) tolerance = 0;

    // Start off with any length difference, which are considered mistakes.
    unsigned int mistakes = (unsigned int)abs(first.length() - second.length());
    // Search only the length of the smaller string.
    const size_t searchLength = (firstGreaterOrEqualToSecond ? second.length() : first.length());

    // Do the search.
    for(size_t i = 0, max = searchLength; i &lt; max; i++)
    {
        if(first.at(i) != second.at(i)) mistakes++;
    }

    // Output the similar characters.
    if(similarCharacters != nullptr) *similarCharacters = (unsigned int)abs(searchLength - mistakes);

    // Compare the mistakes to the tolerance.
    return (mistakes &lt;= tolerance);
}
1. Prefer passing parameters by const reference

The std::string parameters should be passed by const reference rather than by value.

Even if passing by value would work properly, it makes the function signature clearer for the caller semantically and may be more efficient.

2. Fix all warnings

The line 

return (mistakes &lt;= tolerance);


results in a compiler warning:

warning: comparison between signed and unsigned integer expressions [-Wsign-compare]


3. Prefer to use numeric_limits over the C-style INT_MIN

For C++ code you should prefer to use std::numeric_limits&lt;int&gt;::min() instead of the INT_MIN macro (I couldn't even get that to compile, though stdint.h was included).

4. Always use {} braces for conditional code sections

You always should use braces to enclose conditional code sections

if(similarCharacters != nullptr) {
     *similarCharacters = (unsigned int)abs(searchLength - mistakes);
}


Not only it improves the readability of the code, omitting the braces may make the code error prone for changes.



My compiling version can be found here.