Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

C stricmp() Function

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

Photo Credit to CodeToFun

🙋 Introduction

In C programming, comparing strings is a common operation, and there are standard functions for this task.

For case-insensitive string comparison, some compilers provide a non-standard extension called stricmp().

It's essential to note that the use of non-standard functions may result in portability issues.

In this tutorial, we'll explore the usage and functionality of the stricmp() function.

💡 Syntax

The signature of the stricmp() function is typically as follows:

Syntax
Copied
Copy To Clipboard
int stricmp(const char *s1, const char *s2);

This function takes two strings, s1 and s2, and performs a case-insensitive comparison.

📄 Example

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

stricmp.c
Copied
Copy To Clipboard
#include <stdio.h>

// Prototype for stricmp function (may not be needed in some compilers)
int stricmp(const char * s1,
  const char * s2);

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

  // Compare strings case-insensitively
  int result = stricmp(str1, str2);

  // Output the result
  if (result == 0) {
    printf("The strings are equal.\n");
  } else {
    printf("The strings are not equal.\n");
  }

  return 0;
}

đŸ’ģ Output

Output
The strings are equal.

🧠 How the Program Works

In this example, the stricmp() function compares the strings "Hello, World!" and "hello, world!" case-insensitively and prints the result.

↩ī¸ Return Value

The stricmp() function returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2. The comparison is case-insensitive.

📚 Common Use Cases

The stricmp() function is particularly useful when you need to compare strings in a case-insensitive manner. However, be cautious about its use, as it's not a standard C library function and may not be available in all compilers.

📝 Notes

  • Verify whether your compiler supports the stricmp() function. In some environments, you might need to use platform-specific alternatives or consider converting strings to lowercase before comparison.

đŸŽĸ Optimization

The stricmp() function is optimized for case-insensitive string comparison. However, due to its non-standard nature, consider using standard functions or platform-specific alternatives for better code portability.

🎉 Conclusion

The stricmp() function in C is a non-standard extension that provides case-insensitive string comparison. While it may be convenient, be mindful of potential portability issues, and explore standard alternatives or platform-specific solutions for more robust code.

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

đŸ¤¯ Fun Fact

Logo

Did you Know?

For case-insensitive string comparison, the standard library provides strcasecmp() in POSIX systems and stricmp() in Windows.

👨‍đŸ’ģ 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
10 months ago

If you have any doubts regarding this article (C stricmp() 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