C++

Case Insensitive string Compare in C++

Comparison has been a very known and most used concept in many fields. When it comes to programming languages, we used to compare numbers most of the time. But, that’s not the case in all programming languages. Some of the programming languages like C++ provide us the opportunity to compare string type values as well. This can be done by ignoring the case sensitivity of those strings using some built-in methods. Thus, within our today’s article, we will discuss those methods to do a string case insensitive comparison in C++. So, let’s get started by opening the Ubuntu 20.04 terminal console via “Ctrl+Alt+T”.

Example 01: Using Strcasecmp Method

Here we have started our first example by creating a file that must contain the c++ extension. The touch query has been utilized for this specific purpose. After that, a user needs to open the newly created file to add some code to it. A user can utilize any built-in editor of Ubuntu 20.04 like vim, text, or nano editor. So, we have been utilizing the nano editor here as per our ease and quick edit. So, the “nano” keyword is used here to open the “case.cc” file in the Nano editor. Your file will be opened in the empty editor.

We have started the c++ code with some unique and necessary header files. These header files are being included using the keyword “include” with the hash sign. The header files include the “input-output” stream header, “string” header, and “cstring” header to utilize the strcasecmp() method in the code. The standard namespace is essential to further use the cout and cin clauses in the code. The main() method has been in progress with the initialization of two string type variables s1 and s1, with some string values in it. You can see that both strings got almost the same values with a little case insensitivity. The “if” statement is used here to compare two string variables s1 and s2.

The “strcasecmp()” function has been used in the “if” statement and takes both string variables s1 and s2 to be compared. This function will ignore their case and check if both strings are matched or not using a single byte at one time via the “c_str()” method. If the byte of s1 matches with the same place byte of string s2, it will return 0. In the last, if all the results returned equals 0, it shows that the string s1 is similar to s2, ignoring their case. Therefore, the “if” statement will run the first “cout” statement showing that both strings are matched. Otherwise, the other part of the “if” statement will execute and display that the strings are not matched. The return clause will simply end the main() method here. Our program is completed here. Let’s just quickly save it with the “Ctrl+S” shortcut and quit with the “Ctrl+X”. This is a necessary step before going to the compilation and execution phase.

Now that the file has been saved and we are back to the terminal, it’s time to compile it quickly. For the compilation of any c++ file in Ubuntu 20.04, you need a “g++” compiler. If you don’t’ have one, try installing it with the “apt” package. Otherwise, your code won’t result in the way it should. So, we compiled the “case.cc” file with the “g++” compiler, which was successful. After that, the last step was the execution of this compiled file. This has been done with the help of a “./a.out” instruction. We have got the “Strings matched…” message as both the strings s1 and s2 are equal in comparison but different in their case.

Example 02: Using Strncasecmp Method

Let’s take another similar example of comparing two case insensitive strings in C++. This time, we will be utilizing the “strncasecmp()” method instead of the “strcasecmp()” similar function. Both work quite similarly, but the “strncasecmp()” method works a little differently. This function takes 3 arguments while the “strcasecmp()” method takes 2 as we did in the first example. The code is similar to the first example code with a minor change at two positions. The first change has been done in the value of second-string s2, where we have just changed the position of a string content as per our wish. The second change has been done at the “if” statement where we have added the third parameter in the function “strncasecmp()” method. This parameter takes an integer value to specify the number of characters from both the strings to be matched or compared, i.e., first 5. This means, only the first 5 characters from both the strings will be compared, and the result will be generated according to that.

If the comparison got successful and both the strings got the same characters ignoring their case sensitivity, it will return 0, and the first cout statement will be executed. Otherwise, the next cout clause will be executed. Let’s just save with the “Ctrl+S” shortcut and quit the file with “Ctrl+X” to return to the terminal. Now, it’s our turn for the compilation.

After the compilation, the execution command shows that the strings are not matched because the first 5 characters of both strings are different.

Example 03: Using Compare() Method

Let’s have our last but not the least method to compare two case insensitive strings in c++. The code has been started with the header files, i.e., iostream and algorithm. The iostream is used for the input-output stream, but the “algorithm” header is utilized to use the compare() and transform() method in the code properly. After the headers, the standard “namespace” is utilized as necessary for the usage of “cout” and “cin” clauses to display and get the input. Then, we have started our main() function of the integer return type. It is initializing two string-type variables s1 and s2. Both strings contain some string values with different case sensitivity but are similar in the characters.

The “transform()” method has been applied to both strings, s1 and s2, to convert them into the upper case using the function “toupper()” from start to end. After converting, the “compare()” method has been used in the “if” statement to check whether the string s1 is equal to s2. If the comparison of each character byte at the same place of both the strings returns 0, it means both are similar. So, it will execute the first “cout” statement saying the strings are matched. Otherwise, the else part “cout” statement will be executed, showing that the strings are not the same. The code is completed here.

After the compilation and running of the code file, we have got the “Strings matched…” message. Both the strings turned the same after being converted to the Upper case.

Conclusion:

This article is at its best to explain the case insensitive string compare in the C++ language. We have used the three different functions in our examples to achieve this functionality i.e.strcasecmp(), strncasecmp(), transform() and compare(). All the examples are implemented on Ubuntu 20.04 system and are equally executable on other Linux distributions. We hope you will find this article quite helpful while learning C++.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.