C++ strncmp Function Examples
The C++ strncmp function examples is a powerful utility from the C library used to compare portions of two strings. It is part of the <cstring> header and is particularly useful when you only want to compare a specific number of characters, rather than the entire strings. This can be beneficial for optimizing performance or for use cases where string prefixes matter more than complete matches.
In this article, we will explore various examples of using strncmp in practical scenarios.
Syntax
int strncmp(const char *str1, const char *str2, size_t num);
Parameters:
str1: Pointer to the first string to be compared.
str2: Pointer to the second string to be compared.
num: Maximum number of characters to compare.
Return Value:
< 0: str1 is less than str2.
0: The two strings are equal up to num characters.
0: str1 is greater than str2.
Example 1: Basic Comparison
Here is a basic example of comparing two strings using strncmp
include <iostream>
include <cstring>
int main() {
const char *str1 = "apple";
const char *str2 = "apricot";
if (strncmp(str1, str2, 3) == 0) {
std::cout << "The first 3 characters of both strings are equal." << std::endl;
} else {
std::cout << "The first 3 characters of both strings are not equal." << std::endl;
}
return 0;
}
Output:
The first 3 characters of both strings are equal.
This example demonstrates how strncmp compares only the first three characters of the two strings, even if the strings differ afterward.
Example 2: Case Sensitivity
The strncmp function is case-sensitive, meaning that uppercase and lowercase letters are treated as different.
#include <iostream>
include <cstring>
int main() {
const char *str1 = "apple";
const char *str2 = "apricot";
if (strncmp(str1, str2, 3) == 0) {
std::cout << "The first 3 characters of both strings are equal." << std::endl;
} else {
std::cout << "The first 3 characters of both strings are not equal." << std::endl;
}
return 0;
}
This example demonstrates how strncmp compares only the first three characters of the two strings, even if the strings differ afterward.
Example 2: Case Sensitivity
The strncmp function is case-sensitive, meaning that uppercase and lowercase letters are treated as different.
#include <iostream>
include <cstring>
int main() {
const char *str1 = "Hello";
const char *str2 = "hello";
if (strncmp(str1, str2, 5) == 0) {
std::cout << "The strings are equal." << std::endl;
} else {
std::cout << "The strings are not equal." << std::endl;
}
return 0;
}
Conclusion
The strncmp function is a versatile tool for string comparison in C++. By allowing developers to limit comparisons to a specific number of characters, it provides flexibility and efficiency. From validating prefixes to handling user input, strncmp is a valuable addition to any programmer's toolkit. Use it judiciously, keeping in mind its case sensitivity and limitations when working with dynamic or locale-specific strings.
More Visit- https://docs.vultr.com/cpp/standard-library/cstring/strncmp