C++

Strncmp C++

Strings are the main part of the C++ language as it holds a lot of built-in functions to do some specific tasks on those strings. You can create substrings, check their sizes, and relate them to one another. String comparison is the most-used concept in C++. To do this, C++ came up with strncmp() function. It’s quite easy to use with if-else statements in the code. Therefore, we will discuss its working in Ubuntu 20.04 system. Let’s get started with the C++ new file generation with touch instruction on the shell and open it with the “nano” keyword.

Example 01:

Let’s get started with the first example to use the strncmp() function. The code has been started with the C++ library’s inclusion. The header libraries we have included are “iostream” and “ string.h” for the use of the input-output stream and string functions directly in the code. C++ “Std” namespace is necessary to add within the code to make use of “cin” and “cout” statements. All the code has been done within the main() function. So, we started it with the declaration and initialization of two-character type string variables of size 30, i.e. s1 and s3. Both the files contain the string type values in them, i.e. half same and half different. The strncmp() function has been called to compare both the two variables s1 and s2.

This function call has been utilizing both the variables s1 and s2 in the arguments and the number of elements we want to compare, i.e. 9. The number of equal characters for both strings returned by the strncmp() function will be saved to the integer variable “I”. Here comes the “if-else” statement to check whether the returned number of equal characters is zero, less than 0, or greater than 0. If the number “I” is equal to zero, the cout statement will display the string equal output. This means that there are no unmatched characters have been found so far. Otherwise, the else “cout” statement will display that strings are not equal.

After this code compilation with g++ and execution with the “./a.out” command, we have got the result as showing that both the strings are equal up to the first 9 characters in each.

Let’s change the number of characters to be compared between both strings. So, we have been utilizing the same piece of code for C++. We have updated the strncmp() function here by replacing its third argument. We have replaced 9 by 14 to compare the total of the first 14 characters from both strings.

After the updated code compilation and running, we have got the shown below output. As string 1 is different from string 2 onward, its 10th character displays that both strings are not equal after the first 9 characters.

Example 02:

Let’s take a look at another example of the strncmp() function working on different types of strings. This time, we will be having a look at two same character-type strings with different cases. So, we have started this example with the same headers and namespace of C++ before the main() function implementation. Within the main() method, 2 character type strings have been initialized with string values; both are the same in characters but different in type-cases, i.e. upper and lower. The strncmp() method has been called to check if both the strings are equal up to the first 5 characters. The “if-else” statement will work accordingly, i.e. equal or not equal. Let’s save and execute this code to see the update is working properly or not.

We have compiled this code first and executed it after that. In return, we have got that the strings are not equal. This proves that the strncmp() function returns 0 if and only if both strings are the same in characters and type-cases. Otherwise, it will return a value less than or equal to 0.

Therefore, we have updated the string values in the code with the same type-cases and characters. After string initialization, we have called the strncmp() method to compare both the string up to the last character. We have added the nested “if-else” statement here to check more than 1 condition. If the return value is equal to 0, it will return that strings are equal along with the display of returned value. Otherwise, if the comparison value returned is greater or less than 0, it will act accordingly and display that particular returned value.

After the compilation and execution of this updated code, we got the “Strings are Equal” message and the returned value, i.e. 0.

Example 03:

We have done with all the examples using the strncmp() function to see if two strings are equal or not. We will be looking at strncmp() function a little differently this time. We will utilize one large string and one small string in the code. So, we have been starting our example with the iostream and string.h header libraries using the “#include” keyword with both. After adding the namespace, we have been starting the main() function with two string variables initialization.

The string s1 contains more than 5 characters, and string s2 contains only 5. The strncmp() function has been executed with a simple function called passing both variables to its argument. The number of characters to be matched is set to 5. The strncmp() function will compare both variables and return the particular result. As our first string is greater in size than the 2nd string and doesn’t match to 2nd string, therefore the “else-if” part of the statement for “I > 0” will be executed. In return, it will display that “S1 is greater than S2” along with the returned value of “I” for several mismatched characters.

After running this code on the shell, we have got the result as expected, i.e. S1 is greater than S2 On the other hand, the value of “I” is returned as “32”.

Overall code for this example will remain the same as we have done in the above code. The only change we have got here is the exchange of string values with each other. Now, string s2 is greater in size than string s1. In this case, the last else statement must be executed. Let’s run this code now.

After this code compilation and execution, we have seen that the else part of the code has been executed after the strncmp() function call. It shows that the first string is smaller in size than the other string while none of the characters exactly matches.

Conclusion:

At last, we have completed the explanation of the strncmp() function of C++ to compare two strings. We have discussed the strncmp() method with different types of strings, i.e. one greater than the other in size, same strings, and different strings. Simple and nested “if-else” statements have been utilized so far to make our code check different conditions for strings and returned values by an strncmp() method. Due to the simplicity of our examples, we hope that you will find it easy to learn and implement.

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.