std::strncmp() in C++
Last Updated :
29 May, 2023
std::strncmp() function in C++ lexicographically compares not more than count characters from the two null-terminated strings and returns an integer based on the outcome. This function is a Standard Library function that is defined in <cstring> header file in C++.
Syntax
int strncmp(const char *str1, const char *str2, size_t count);
Parameters
- str1 and str2: Pointer to the strings to be compared.
- count: Maximum number of characters to be compared.
- size_t: It is an unsigned integral type.
Return Value
- It returns 0 if str1 is equal to str2.
- It returns an integer value less than zero if str1 is less than str2.
- It returns an integer value greater than zero if str1 is greater than str2.
If there are fewer than count characters in either string, the comparison ends when the first null is encountered.
Example of strncmp()
C++
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
char str1[10] = "akash" ;
char str2[10] = "aksh" ;
int result = strncmp (str1, str2, 4);
if (result == 0) {
cout << "str1 is equal to str2 up to num characters"
<< endl;
}
else if (result > 0)
cout << "str1 is greater than str2" << endl;
else
cout << "str2 is greater than str1" << endl;
cout << "Value returned by strncmp() is: " << result
<< endl;
return 0;
}
|
Output
The first 5 characters are same.
What does strcmp() function return?
When the strings are not equal, the value returned by the strncmp() function is the difference between the ASCII values of the first unmatched character in str1 and str2.
The strncmp() function returns three different types of integer values on the basis of comparison.
1. Greater than zero ( >0 )
A positive value is returned if a character of str1 and str2 doesn’t match before the num characters and the ASCII value of the str1 character is greater than the ASCII value of the str2 character. As a result, we can say that str1 is lexicographically greater than str2.
Example
C++
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
char str1[10] = "akash" ;
char str2[10] = "akas" ;
int result = strncmp (str1, str2, 4);
if (result == 0) {
cout << "str1 is equal to str2 up to num characters"
<< endl;
}
else if (result > 0)
cout << "str1 is greater than str2" << endl;
else
cout << "str2 is greater than str1" << endl;
cout << "Value returned by strncmp() is: " << result
<< endl;
return 0;
}
|
Output
str1 is greater than str2
Value returned by strncmp() is: 18
2. Less than zero ( <0 )
A negative value is returned, if a character of str1 and str2 doesn’t match before the num characters and the ASCII value of the str1 character is lesser than the ASCII value of the str2 character. As a result, we can say that str2 is lexicographically greater than str1.
Example
Output
str2 is greater than str1
Value returned by strncmp() is: -18
3. Equal to zero ( 0 )
This function returns zero if the characters of str1 match with the characters of the str2 up to num characters. As a result, we cannot say that str1 is equal to str2 until num is equal to the length of either string.
Example
C++
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
char str1[10] = "akash" ;
char str2[10] = "akas" ;
int result = strncmp (str1, str2, 4);
if (result == 0) {
cout << "str1 is equal to str2 up to num characters"
<< endl;
}
else if (result > 0)
cout << "str1 is greater than str2" << endl;
else
cout << "str2 is greater than str1" << endl;
cout << "Value returned by strncmp() is: " << result
<< endl;
return 0;
}
|
Output
str1 is equal to str2 upto num characters
Value returned by strncmp() is: 0
Important Points
- This function takes two strings and a number num as arguments and compares at most the first num bytes of both strings.
- num should be at most equal to the length of the longest string. If num is defined as greater than the string length then the comparison is done till the null character (‘\0’) of either string.
- This function compares the two strings lexicographically. It starts comparison from the first character of each string. If they are equal to each other, it continues and compares the next character of each string, and so on.
- This process of comparison stops until a terminating null character of either string is reached or the num characters of both strings match.
Related Articles
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...