Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

C++ String strncmp() Function

Posted in C++ Tutorial
Updated on Oct 06, 2024
By Mari Selvan
👁ī¸ 347 - Views
âŗ 4 mins
đŸ’Ŧ 1 Comment
C++ String strncmp() Function

Photo Credit to CodeToFun

🙋 Introduction

In C++ programming, comparing strings is a common operation, and the C++ Standard Library provides the strncmp() function for this purpose.

This function allows you to compare substrings of two strings up to a specified length.

In this tutorial, we'll explore the usage and functionality of the strncmp() function in C++.

💡 Syntax

The signature of the strncmp() function is as follows:

Syntax
Copied
Copy To Clipboard
int strncmp(const char *str1, const char *str2, size_t n);

This function compares the first n characters of two C-style strings (str1 and str2).

📄 Example

Let's delve into an example to illustrate how the strncmp() function works.

strncmp.cpp
Copied
Copy To Clipboard
#include <iostream>
#include <cstring>

int main() {
  const char * str1 = "Hello, World!";
  const char * str2 = "Hello, C++!";

  // Compare the first 5 characters case-sensitively
  int result = strncmp(str1, str2, 5);

  // Output the result
  if (result == 0) {
    std::cout << "The substrings are equal.\n";
  } else {
    std::cout << "The substrings are not equal.\n";
  }

  return 0;
}

đŸ’ģ Output

Output
The substrings are equal.

🧠 How the Program Works

In this example, the strncmp() function compares the first 5 characters of the strings "Hello, World!" and "Hello, C++!" and prints the result.

↩ī¸ Return Value

The strncmp() function returns an integer less than, equal to, or greater than zero if the first n characters of str1 are found, respectively, to be less than, to match, or be greater than the first n characters of str2.

📚 Common Use Cases

The strncmp() function is useful when you need to compare substrings of two strings up to a specific length. It's commonly employed in scenarios where you want to check the equality of a portion of two strings.

📝 Notes

  • The comparison is case-sensitive. If you need a case-insensitive comparison, consider using strncasecmp() in POSIX systems or a custom case-insensitive comparison function.

đŸŽĸ Optimization

The strncmp() function is optimized for efficient substring comparison. Ensure that the specified length n does not exceed the length of the shorter string to avoid undefined behavior.

🎉 Conclusion

The strncmp() function in C++ is a versatile tool for comparing substrings of two strings up to a specified length. It provides a standardized and efficient way to perform substring comparisons, enhancing the flexibility of your code.

Feel free to experiment with different strings and explore the behavior of the strncmp() function in various scenarios. Happy coding!

👨‍đŸ’ģ Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
9 months ago

If you have any doubts regarding this article (C++ String strncmp() Function), please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy