Site icon DataFlair

Difference Between strcmp() and strcmpi() Function in C

strcmp() vs strcmpi() in c

String manipulation is a fundamental aspect of C programming, and one common task is comparing strings. Whether you’re searching for a specific substring, sorting a list of names, or validating user inputs, string comparison is at the heart of many operations.

In C, two primary functions, strcmp() and strcmpi(), come into play when it comes to comparing strings, but they serve distinct purposes.

The strcmp() function is a stalwart for case-sensitive string comparisons. It meticulously examines every character within two strings, discerning even the slightest variations. On the other hand, the strcmpi() function offers a more relaxed approach, performing case-insensitive comparisons, where ‘A’ is equivalent to ‘a,’ and ‘B’ is equal to ‘b.’

In this article, we’ll delve into the intricacies of these two C library functions, comparing their strengths and weaknesses. We’ll explore their usage, behavior, and the scenarios in which one should be chosen over the other.

By the end, you’ll have a clear understanding of when to employ strcmp() and when strcmpi() is the better option for your string comparison needs. Let’s embark on this journey of understanding case-sensitive and case-insensitive string comparisons in the world of C programming.

The strcmp() Function in C

When comparing two strings, the strcmp() function takes into account case. Based on the ASCII value of each character, it compares strings character by character.

The syntax of strcmp() is:

int strcmp(const char *str1, const char *str2);

It returns:

Let’s look at an example:

#include <stdio.h>
#include <string.h>

int main() {
  char str1[15] = "String1";
  char str2[15] = "String2";

  int result = strcmp(str1, str2);

  if(result < 0) {
    printf("str1 is less than str2\n"); 
  }
  else if(result > 0) {
    printf("str1 is greater than str2\n");
  }
  else {
    printf("str1 is equal to str2\n");
  }
  
  return 0;
}

Output:
str1 is less than str2

This prints “str1 is less than str2” as string comparison is case-sensitive.

Some key properties of strcmp():

The strcmpi() Function in C

The “string.h” header file contains the definition of the strcmpi() function, a built-in function in the C language. The only distinction between the strcmpi() and strcmp() functions is that the former is case sensitive while the latter is not, making strcmpi() identical to strcmp().

The syntax is:

int strcmpi(const char *str1, const char *str2);

This function accepts two string parameters to compare:

The function returns an integer value indicating the result of the comparison:

Return Value Meaning
0 str1 and str2 are identical
Negative number str1 length < str2 length
Positive number str1 length > str2 length

Examples

str1 str2 Return Value Reason
“hello” “hello” 0 Strings are identical
“cat” “tiger” -3 “cat” length (3) < “tiger” length (5)
“antelope” “deer” 4 “antelope” length (8) > “deer” length (4)

Note: The strcmpi() function is a non-standard one that might not be found in the C standard library.

Let’s look at an example:

#include <stdio.h>
#include <string.h>

int main() {

  char str1[15] = "String1";
  char str2[15] = "string1";

  int result = strcmpi(str1, str2);

  if(result == 0) {
    printf("str1 is equal to str2\n");
  }

  return 0;
}

Output:
str1 is equal to str2

This prints “str1 is equal to str2” as case is ignored by strcmpi().

Some key points about strcmpi():

Key Differences Between strcmp() and strcmpi() in C

Feature strcmp() strcmpi()
Case sensitivity Case-sensitive comparison Case-insensitive comparison
Normalization No normalization Normalizes to same case before comparing
Comparison method Directly compares ASCII values Normalizes case, then compares ASCII values
Performance Faster as direct comparison Slower due to normalization
Return value Negative if str1 < str2, Positive if str1 > str2, 0 if equal 0 if equal, non-zero if unequal
Use cases Sorting, finding min/max strings User input validation, configuration value comparison

When to Use Each Function

Based on their behavior, here are some guidelines on when to use each function:

Conclusion

String comparison is fundamental to many C programs. Both strcmp() and strcmpi() allow for the comparison of two strings, with the main difference being that strcmpi() ignores case. Keeping their exact behavior in mind helps us choose the right function based on whether case-sensitivity matters for our specific use case.

Exit mobile version