compare-similar-strings-using-strcmp
compare-similar-strings-using-strcmp

How to Compare Two Strings in C

In C, comparing strings is a crucial operation for various tasks, from sorting data to validating user input. Unlike some languages with built-in string comparison operators, C requires the use of the strcmp() function from the string.h library. This function provides a lexicographical comparison of two strings.

Understanding strcmp()

The strcmp() function compares two strings character by character based on their ASCII values. It takes two arguments:

  • s1: The first string to compare.
  • s2: The second string to compare.

The function returns an integer value indicating the relationship between the two strings:

  • 0: The strings are identical.
  • Positive value ( > 0 ): The first non-matching character in s1 has a greater ASCII value than the corresponding character in s2. This means s1 is lexicographically greater than s2.
  • Negative value ( < 0 ): The first non-matching character in s1 has a lesser ASCII value than the corresponding character in s2. This means s1 is lexicographically smaller than s2.

How strcmp() Works

strcmp() iterates through both strings simultaneously, comparing characters at each corresponding position. The comparison continues until:

  1. A mismatch is found: The function returns a positive or negative value based on the ASCII difference of the mismatched characters.
  2. The end of either string is reached (null terminator ”): If all characters up to this point are equal, the function returns 0.

Examples of strcmp() in Action

Let’s illustrate with a few examples:

1. Comparing Identical Strings:

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

int main() {
  char str1[] = "hello";
  char str2[] = "hello";
  int result = strcmp(str1, str2);
  printf("Comparison result: %dn", result); // Output: 0
  return 0;
}

2. Comparing Strings with Different Cases:

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

int main() {
  char str1[] = "Hello";
  char str2[] = "hello";
  int result = strcmp(str1, str2);
  printf("Comparison result: %dn", result); // Output: Negative value (e.g., -32)
  return 0;
}

Remember, strcmp() is case-sensitive.

3. Comparing Strings with Different Lengths:

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

int main() {
    char str1[] = "apple";
    char str2[] = "applesauce";
    int result = strcmp(str1, str2);
    printf("Comparison result: %dn", result); // Output: Negative value
    return 0;
}

Practical Applications of strcmp()

strcmp() finds use in various scenarios:

  • Sorting strings alphabetically: strcmp() can be used as a comparator function with sorting algorithms like qsort().
  • Validating user input: Comparing user-entered passwords against stored credentials.
  • Searching for strings in a list: Determining if a specific string exists within an array of strings.

Conclusion

strcmp() is a fundamental function for string manipulation in C. Understanding its behavior and return values is essential for writing correct and efficient C code. By leveraging strcmp(), you can perform a wide range of string comparisons and implement more complex string-based logic.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *