strcmpi in C++
introduction:
Strcmpi is not a standard library function in C++. However, it appears that you may be referring to a particular function that is accessible as a non-standard extension in some C or C++ compilers. Use of this function for case-insensitive string comparison is common.
Example:
#include <iostream>
#include <cstring>
int main() {
const char* str1 = "Hello";
const char* str2 = "hello";
int result = _strcmpi(str1, str2);
if (result == 0) {
std::cout << "The strings are equal (case-insensitive)." << std::endl;
} else {
std::cout << "The strings are not equal (case-insensitive)." << std::endl;
}
return 0;
}
Output:
Different ways to implement:
1.Custom Function Using Standard Library:
Example:
#include <iostream>
#include <string>
#include <algorithm>
bool strcmpi(const std::string& str1, const std::string& str2) {
if (str1.length() != str2.length()) {
return false;
}
return std::equal(str1.begin(), str1.end(), str2.begin(), str2.end(),
[](char a, char b) {
return std::tolower(a) == std::tolower(b);
});
}
int main() {
std::string str1 = "Hello";
std::string str2 = "hello";
if (strcmpi(str1, str2)) {
std::cout << "The strings are equal (case-insensitive)." << std::endl;
} else {
std::cout << "The strings are not equal (case-insensitive)." << std::endl;
}
return 0;
}
Output:
Explanation:
- It contains the necessary headers for the C++ Standard Library, including, and.
- It defines a special strcmpi function that accepts two std::string objects as arguments and returns a bool that determines, case-insensitively, if the strings are equal. First, the function checks the lengths of the two strings, returning false if the lengths vary. If the lengths are equal, it employs std::equal and a lambda function to evaluate each letter case-insensitively while converting characters to lowercase first using std::tolower.
- With the values "Hello" and "hello," str1 and str2 are two std::string objects that are declared. To compare str1 and str2, it invokes the unique strcmpi function. It prints any of the following messages depending on the outcome of the comparison
- The strings match regardless of case. if the strcmpi function returns a value of true, then the strings are equivalent regardless of case.
- The strings aren't equal (case is not taken into account). If the strcmpi function yields a false result, then the strings are not equal when considering case.
2.Use C Standard Library Functions:
Example:
#include <iostream>
#include <cstring>
#include <cctype>
int strcmpi(const char* str1, const char* str2) {
while (*str1 && *str2) {
if (std::tolower(*str1) != std::tolower(*str2)) {
return (*str1 - *str2);
}
str1++;
str2++;
}
return (*str1 - *str2);
}
int main() {
const char* str1 = "Hello";
const char* str2 = "hello";
int result = strcmpi(str1, str2);
if (result == 0) {
std::cout << "The strings are equal (case-insensitive)." << std::endl;
} else {
std::cout << "The strings are not equal (case-insensitive)." << std::endl;
}
return 0;
}
Output:
Explanation:
- It contains important C++ Standard Library headers like "iostream" and "cstring" as well as headers from the C Standard Library like "cctype."
- With the values "Hello" and "hello," str1 and str2 are two C-style strings that are declared. To compare str1 and str2, it invokes the unique strcmpi function. It prints one of the following messages depending on the outcome of the comparison:
- The strings match regardless of case. If the strcmpi function returns a value of 0, it means that the strings are equal regardless of case.
- The strings are not equal (case is not taken into account). if the case-insensitive strcmpi method returns a non-zero value, indicating the characters are not equal.
3. Use a Third-Party Library:
Example:
#include <iostream>
#include <boost/algorithm/string.hpp>
int main() {
std::string str1 = "Hello";
std::string str2 = "hello";
if (boost::iequals(str1, str2)) {
std::cout << "The strings are equal (case-insensitive)." << std::endl;
} else {
std::cout << "The strings are not equal (case-insensitive)." << std::endl;
}
return 0;
}
Output:
Explanation:
- It contains the header file required for using Boost's string algorithms as well as input and output operations.
- With the values "Hello" and "hello," str1 and str2 are two std::string objects that are declared.
- The outcome of the boost::iequals function call is tested using the if statement. The Boost library offers the case-insensitive string comparison function boost::iequals.
- If boost::iequals returns true, it indicates that the two strings are equivalent when compared without regard to case. In this instance, std::cout is used to output the message "The strings are equal (case-insensitive)".
Limitations :
- Compiler-Specific: The extensions _strcmpi and _stricmp are compiler-specific additions and not a part of the C++ Standard Library. As a result, they might not be supported by all C++ compilers, which would make your code less portable.
- These functions are not standardised, therefore they might act differently under different compilers. They are unreliable for cross-platform programming due to their unpredictable exact behaviour and availability.
- Limited Locale Support: These functions primarily compare items based on case-insensitivity and the ASCII character set, which may not be accurate for characters that aren't English or in other locales. They don't offer strong internationalisation support.
- These methods anticipate null-terminated C-style strings, which raises the possibility of buffer overflows. Buffer overflows and unexpected behaviour can occur if the input strings are not null-terminated or when they are too long for the allocated buffer.
- Limited Functionality: The case-insensitive string comparison functions are the exclusive focus of the _strcmpi and _stricmp libraries. If you require more sophisticated or unique comparison logic (for example, case-folding for characters other than ASCII or locale-specific comparison logic), you may need to develop your own solution or use common C++ libraries.
- Not Type-Safe: The type safety and robustness that may be obtained using C++'s std::string and related classes are not provided by these functions, which operate on character arrays in the C style (C-strings).