How strcmp works in C programming

The strcmp function in C is used to compare two strings. It is a standard library function that allows you to determine whether two strings are equal, or if one string is lexicographically less than or greater than the other. This blog will explain how strcmp works and how you can use it effectively.

How strcmp Works

The strcmp function compares two null-terminated strings character by character. Here’s the syntax for strcmp:

int strcmp(const char *str1, const char *str2);
  • str1: Pointer to the first null-terminated string.
  • str2: Pointer to the second null-terminated string.

Return Value:

  • Zero (0): If both strings are equal.
  • Negative Value (< 0): If the first string is less than the second string.
  • Positive Value (> 0): If the first string is greater than the second string.

The comparison is done using the ASCII values of the characters. The function stops comparing as soon as a difference is found or the end of the strings is reached.

Example of strcmp:

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

int main() {
    char str1[] = "Hello";
    char str2[] = "Hello";
    char str3[] = "World";

    int result1 = strcmp(str1, str2);
    int result2 = strcmp(str1, str3);

    printf("Result of strcmp(str1, str2): %d\n", result1); // Should be 0
    printf("Result of strcmp(str1, str3): %d\n", result2); // Should be negative

    return 0;
}

Possible Ways to Use strcmp

  1. Checking String Equality:
   if (strcmp(str1, str2) == 0) {
       printf("The strings are equal.\n");
   }
  1. Sorting Strings:
    strcmp can be used as a comparator function in sorting algorithms to sort an array of strings.
   #include <stdio.h>
   #include <string.h>

   void sortStrings(char arr[][100], int n) {
       char temp[100];
       for (int i = 0; i < n - 1; i++) {
           for (int j = i + 1; j < n; j++) {
               if (strcmp(arr[i], arr[j]) > 0) {
                   strcpy(temp, arr[i]);
                   strcpy(arr[i], arr[j]);
                   strcpy(arr[j], temp);
               }
           }
       }
   }

   int main() {
       char arr[5][100] = {"Banana", "Apple", "Cherry", "Mango", "Blueberry"};
       int n = 5;

       sortStrings(arr, n);

       printf("Sorted array of strings:\n");
       for (int i = 0; i < n; i++) {
           printf("%s\n", arr[i]);
       }

       return 0;
   }
  1. Finding Lexicographical Order:
    strcmp can be used to determine the lexicographical order of strings.
   if (strcmp(str1, str2) < 0) {
       printf("%s comes before %s\n", str1, str2);
   } else if (strcmp(str1, str2) > 0) {
       printf("%s comes after %s\n", str1, str2);
   } else {
       printf("%s and %s are equal\n", str1, str2);
   }
  1. Command Line Argument Comparison:
    strcmp is useful for comparing command line arguments.
   #include <stdio.h>
   #include <string.h>

   int main(int argc, char *argv[]) {
       if (argc < 2) {
           printf("No arguments provided.\n");
           return 1;
       }

       if (strcmp(argv[1], "-help") == 0) {
           printf("Help information...\n");
       } else if (strcmp(argv[1], "-version") == 0) {
           printf("Version 1.0\n");
       } else {
           printf("Unknown argument: %s\n", argv[1]);
       }

       return 0;
   }

Conclusion

The strcmp function is a powerful and versatile tool in C for comparing strings. Whether you need to check for equality, sort strings, or determine their lexicographical order, strcmp provides a straightforward and efficient solution. Understanding how to use strcmp effectively will enhance your string manipulation capabilities in C programming.

Happy coding!

Leave a Comment

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

Scroll to Top