strncmp c++

strncmp c++

 Strings are a core part of the C++ language, as it contains many built-in functions to perform specific tasks on those strings. You can create substrings, check their sizes, and link them together. String comparison is the most commonly used concept in C++. To do this, C++ introduced the strncmp() function. It's pretty easy to use with if-else statements in code. Therefore, we will discuss how it works on an Ubuntu 20.04 system. Let's start by creating a new shelled touch instruction C++ file and opening it with the "nano" keyword.

strncmp c++

Example 1

Let's start with the first example of using the strncmp() function. The code is run with the C++ library included. We have included the "iostream" and "string.h" header libraries for using the I/O stream and string functions directly in the code. The C++ namespace "Std" must be added to the code in order to use the "cin" and "cout" operators. All code has been executed in the main() function. So, we started it by declaring and initializing two-character type string variables of size 30, i.e. s1 and s3. Both files contain string type values, i.e. half the same and half different. The strncmp() function was called to compare two variables s1 and s2.

This function call uses both the s1 and s2 variables as arguments, and the number of elements we want to compare, which is 9. The number of identical characters for both strings returned by the strncmp() function will be stored in an integer variable. "I AM". Here comes the "if-else" statement to check if the number of identical characters returned is zero, less than 0 or greater than 0. If the number "I" is zero, the cout statement will display a string equal to the output. This means that so far no unsurpassed symbols have been found so far. Otherwise, the else "cout" statement will show that the strings are not equal.

 

Example 1

After compiling this code with g++ and executing it with the "./a.out" command, we got a result showing that both strings are equal up to the first 9 characters of each.

 

Example 1

Let's change the number of characters to compare between both strings. So, we used the same piece of code for C++. Here we have updated the strncmp() function by replacing its third argument. We changed 9 to 14 to compare the sum of the first 14 characters from both strings.

 

Example 1

After compiling and running the updated code, we got the following output. Since line 1 is different from line 2 onwards, its 10th character indicates that both strings are not equal after the first 9 characters.

Example 1

Example 2

Let's take a look at another example of the strncmp() function that works with different types of strings. This time we'll look at two strings of the same character type with different case. So we started this example with the same C++ headers and namespace before main() was implemented. In the main() method, 2 strings of character types were initialized with string values; both are the same in characters, but different in case, i.e. top and bottom. The strncmp() method was called to check if both 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 run this code to see if the update works correctly or not.

 

Example 2

We first compiled this code and then ran it. Instead, we got that the strings are not equal. This proves that the strncmp() function returns 0 if and only if both strings match in characters and case. Otherwise, it will return a value less than or equal to 0.

Example 2

Therefore, we updated the string values in the code with the same cases and symbols. After the string was initialized, we called the strncmp() method to compare the strings up to the last character. We've added a nested if-else statement here to test more than one condition. If the return value is 0, it will return that the strings are equal along with displaying the return value. Otherwise, if the comparison's return value is greater than or less than 0, it will act accordingly and display that particular return value.

 

Example 2

After compiling and running this updated code, we got the message "Strings are Equal" and the return value, i.e. 0.Example 2

Example 3

We are done with all the examples, using the strncmp() function to see if two strings are equal or not. This time we'll look at the strncmp() function a little differently. We will use one big line and one small line in the code. So we started our example with the iostream and string.h header libraries, using the "#include" keyword for both. After adding the namespace, we run the main() function with two string variables initialized.

The string s1 contains more than 5 characters, while the string s2 contains only 5. The strncmp() function was made with a simple function called passing both variables into its argument. The number of matching characters is 5. The strncmp() function compares both variables and returns a specific result. Since our first line is larger than the second line and does not match the second line, the "else-if" part of the statement for "I > 0" will be executed. In response, it will display that "S1 is greater than S2" along with a return value of "I" for multiple mismatched characters.

Example 3

After running this code in the shell, we got the expected result i.e. S1 is greater than S2. On the other hand, the value "I" is returned as "32".

Example 3

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

 

Example 3
After compiling and executing this code, we saw that the rest of the code was executed after calling the strncmp() function. It shows that the first string is smaller than the other string, even though none of the characters match exactly.

 

Example 3

Conclusion

Finally, we have completed the explanation of the C++ strncmp() function for comparing two strings. We have discussed the strncmp() method with different types of strings, i.e. one is larger than the other in size, same rows and different rows. So far, we've used simple and nested if-else statements to have our code test various conditions on strings and return values using the strncmp() method. Due to the simplicity of our examples, we hope that it will be easy for you to learn and implement them.


Please leave your comment to encourage us

Previous Post Next Post