| by Arround The Web | No comments

Case-Insensitive String Comparison in C++

To determine if two strings are the same or different, string comparison is a frequent operation in C++. Case-insensitive string comparisons, in which the uppercase and lowercase letters are not taken into account, may be required in particular circumstances. In a case-insensitive string comparison, all capital and lowercase characters are treated equally.

A case-insensitive comparison, for instance, might treat the “hello” and “HELLO” strings as equivalent. This can be helpful in a variety of circumstances including those involving human input, file systems, and database queries. There are numerous approaches to case-insensitive string comparison in C++.

Before contrasting the strings, one technique is to lowercase (or uppercase) the strings. Using a library function which is made expressly for case-insensitive string comparisons such as stricmp(), strcasecmp(), _stricmp(), or _stricmp_l() is another option.

Declaration of Case-Insensitive String Comparison in C++

Case-insensitive string comparisons are possible using several C++ methods. The most often used C++ standard library functions are stricmp(), strcasecmp(), _stricmp(), and _stricmp_l(). Depending on the function that is used, there may be minor changes in the syntax for these functions. In C++, a case-insensitive string comparison function often has the following syntax:

The two strings to be compared are sent as inputs to the “str1” and “str2” methods. The function outputs an integer value that represents the comparison’s outcome. If “str1” is less than “str2”, the function returns a negative number. If “str1” is equivalent to “str2”, the method returns 0. The method provides a true result if “str1” is higher than “str2”. Regardless of how the function operates, a different precise number might be returned. The function disregards the case of the characters in the strings to carry out a case-insensitive string comparison. As a result, both capital and lowercase characters have the same meaning.

Using the Strcasecmp() Function to Perform a Case-Insensitive String Comparison

Now, let’s start implementing the example where we utilize the strcasecmp() function of string in C++ programming language. In the following example, we first include the necessary headers for input/output and string handling, respectively. The “using namespace std” line is included to avoid having to prefix the standard library functions with the std:: namespace.

#include <iostream>
#include <cstring>
using namespace std;
int main() {
    const char* string_1 = "Hello, world!";
    const char* string_2 = "hello, WORLD!";
    int result = strcasecmp(string_1, string_2);
    if (result == 0) {
        cout << "The two strings are equal in a case-insensitive manner." << endl;
    }
    else if (result < 0) {
        cout << "The first string is less than the second string in a case-insensitive manner." << endl;
    }
    else {
        cout << "The first string is greater than the second string in a case-insensitive manner." << endl;
    }
    return 0;
}

Now, we first call the main() function so that we can write the program in the main() function’s body and execute it successfully. First, we create the two-character type constant variables. Both the const char* pointers named string_1 and string_2 that we define point to two strings with varying letter capitalization. The strings cannot be changed because they are declared as constants, so take note of that.

Then, the strcasecmp() function is used which compares the two strings without considering the case, with the “string_1” and “string_2” inputs. The outcome of the comparison is kept in a result integer variable.

To assess the outcome of the comparison and print the relevant message, we use the “if” statements to check every condition. The program shows a message which states that the two strings are equivalent when compared without taking into account the case if the result variable is equal to 0.

“String_1” is less than “string_2” in a case-insensitive way if the result variable is less than 0, and the program produces a message to that effect. The program produces a message which informs the user that “string_1” is superior to “string_2” in all cases if the result variable is larger than 0 at the end of the main() function and outputs a value of 0 to denote the successful completion of the program as you see in the following illustration:

Comparison of Two Strings Entered by the User in a Case-Insensitive Manner

Here is another example where we compare the string which is entered by the user in C++ programming language. First, we include the basic header files in the example. Then, we move further. We also use the “cstring” library so that we can easily compare the inputted strings. Then, we enter into the main() function body. To hold the two strings that are entered by the user, the program first declares two character arrays – “string_1” and “string_2” – each of size 20. The “Enter first string:” message is then printed to the console by the program, prompting the user to enter the first string.

When the user enters a string, the cin object reads it and uses the >> operator to place it in the “string_1” array. By printing the “Enter second string:” message to the console, the program similarly asks the user to enter the second string. The second string is entered by the user and is read by the cin object before it is saved in the “string_2 array”.

#include <iostream>
#include <cstring>
using namespace std;
int main() {
    char string_1[20], string_2[20];
    cout << "Enter first string: ";
    cin >> string_1;
   
    cout << "Enter second string: ";
    cin >> string_2;
   
    int result = strcasecmp(string_1, string_2);
    if (result == 0) {
        cout << "The two strings are equal in a case-insensitive manner." << endl;
    }
    else if (result < 0) {
        cout << "The first string is less than the second string in a case-insensitive manner." << endl;
    }
    else {
        cout << "The first string is greater than the second string in a case-insensitive manner." << endl;
    }
    return 0;
}

Next, the strcasecmp() method is then used, with the two strings that are stored in “string_1” and “string_2” being sent in as arguments. The strcasecmp() function performs a case-insensitive comparison of the two strings and then outputs the result as an integer number.

Then, the program employs an if-else statement to examine the outcome of the comparison after it is performed. The program outputs the message, “The two strings are equal in a case-insensitive manner”, to the console if the result is 0 which indicates that the two strings are equal regardless of the case. The program writes the message, “The first string is less than the second string in a case-insensitive manner”, to the console if the outcome is less than 0. The program writes the message, “The first string is greater than the second string in a case-insensitive manner”, to the console if the outcome is larger than 0. The program ends with a return value of 0 which denotes the program success.

Conclusion

We learned what the case-insensitive string in C++ language is and how we compare the case-insensitive strings in C++ programming language. We learned the implementation style and illustrated some examples with deep detail of every line code.

Share Button

Source: linuxhint.com

Leave a Reply