Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

C++ String strcmp() Function

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

Photo Credit to CodeToFun

🙋 Introduction

In C++ programming, comparing strings is a common operation, and the strcmp() function is a standard library function designed for this purpose.

The strcmp() function compares two strings lexicographically.

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

💡 Syntax

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

Syntax
Copied
Copy To Clipboard
int strcmp(const char* str1, const char* str2);

This function takes two C-style strings, str1 and str2, and returns an integer value representing their lexicographical relationship.

📄 Example

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

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

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

  // Compare strings lexicographically
  int result = std::strcmp(str1, str2);

  // Output the result
  if (result == 0) {
    std::cout << "The strings are equal." << std::endl;
  } else if (result < 0) {
    std::cout << "str1 is lexicographically less than str2." << std::endl;
  } else {
    std::cout << "str1 is lexicographically greater than str2." << std::endl;
  }

  return 0;
}

đŸ’ģ Output

Output
str1 is lexicographically greater than str2.

🧠 How the Program Works

In this example, the strcmp() function compares the strings "Hello, World!" and "Hello, C++!" lexicographically and prints the result.

↩ī¸ Return Value

The strcmp() function returns an integer less than, equal to, or greater than zero if str1 is found, respectively, to be less than, to match, or be greater than str2.

📚 Common Use Cases

The strcmp() function is useful when you need to compare strings based on their lexicographical order. It's commonly employed in sorting algorithms, searching algorithms, and other scenarios where string ordering matters.

📝 Notes

  • The comparison is case-sensitive. For case-insensitive comparison, consider using std::stricmp() or other platform-specific alternatives.
  • Always ensure that the strings being compared are null-terminated to avoid undefined behavior.

đŸŽĸ Optimization

The strcmp() function is optimized for efficient string comparison. For performance-critical applications, consider other alternatives or optimizations based on specific requirements.

🎉 Conclusion

The strcmp() function in C++ is a fundamental tool for lexicographically comparing strings. It provides a standardized and efficient way to perform this task, contributing to the clarity and reliability of your code.

Feel free to experiment with different strings and explore the behavior of the strcmp() 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 strcmp() 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