w3resource

C strcmp() function

C strcmp() function - compare two strings

The strcmp() function is used to compare two given strings.

Use strcmp() function when:

  • Checking for string equality, such as verifying if a user's input matches a password.
  • Comparing strings lexicographically, such as sorting strings or checking the alphabetical order.
  • Detecting if one string precedes another, like checking if one version of text comes before another.

Syntax:

int strcmp(const char *string1, const char *string2)

Parameters:

Name Description Required /Optional
string1 Null-terminated strings to compare. Required
string2 Null-terminated strings to compare. Required

Return value from strcmp()

Name Value
string1 is less than string2 < 0
string1 is identical to string2 0
string1 is greater than string2 >0

Examples: strcmp() function

Example 1: Comparing pairs of strings to determine their lexical relationship

The existing example demonstrates the strcmp() function by comparing pairs of strings to determine their lexical relationship. It prints whether each pair of strings is equal, if the first is less than the second, or if the first is greater than the second. This example shows how strcmp() can be used to make such comparisons between different strings.

Code:

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

int main()
{
   char str1[] = "1234567890";
   char str2[] = "1234567890";
   int result;
   printf("Original text:");
   printf("\n%s",str1);
   printf("\n%s",str2);
   printf( "\nCompare the said two strings:");
   result = strcmp(str1, str2);
   if( result < 0 )
      printf( "\nFirst is less than second.\n" );
   else if( result == 0 )
      printf( "\nFirst is equal to second.\n" );
   else
      printf( "\nFirst is greater than second.\n" );
   char str3[] = "12345678901";
   char str4[] = "12345678900";
   printf("\nOriginal text:");
   printf("\n%s",str3);
   printf("\n%s",str4);
   printf( "\nCompare the said two strings:");
   result = strcmp(str3, str4);
   if( result < 0 )
      printf( "\nFirst is less than second.\n");
   else if( result == 0 )
      printf( "\nFirst is equal to second.\n");
   else
      printf( "\nFirst is greater than second.\n");   
      
   char str5[] = "12345678901100345678";
   char str6[] = "12345678901297650033";   
   printf("\nOriginal text:");
   printf("\n%s",str5);
   printf("\n%s",str6);
   result = strcmp(str5, str6);
   if( result < 0 )
      printf( "\nFirst is less than second.\n");
   else if( result == 0 )
      printf( "\nFirst is equal to second.\n");
   else
      printf( "\nFirst is greater than second.\n");         
return 0;
}

Output:

Original text:
1234567890
1234567890
Compare the said two strings:
First is equal to second.

Original text:
12345678901
12345678900
Compare the said two strings:
First is greater than second.

Original text:
12345678901100345678
12345678901297650033
First is less than second.

Example 2: Checking for Password Match

This example demonstrates using strcmp() to check if the user's input matches a predefined password.

Code:

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

int main() {
    // Define the correct password
    char correct_password[] = "Secret1$23";
    char user_input[20];

    // Prompt user to enter password
    printf("Enter password: ");
    scanf("%19s", user_input);

    // Compare user input with correct password
    if (strcmp(user_input, correct_password) == 0) {
        printf("Access granted!\n");
    } else {
        printf("Access denied!\n");
    }

    return 0;
}

Output:

Enter password: abcd123
Access denied!
Enter password: Secret1$23
Access granted!

Explanation:

  • A correct_password string is defined as " Secret1$23".
  • The user is prompted to enter a password, which is stored in user_input.
  • strcmp() compares user_input with correct_password. If they match, access is granted; otherwise, it is denied.

Example 3: Sorting two strings lexicographically

This example uses strcmp() to determine the alphabetical order of two strings and prints them in ascending order.

Code:

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

int main() {
    // Define two strings
    char str1[] = "apple";
    char str2[] = "banana";

    // Compare the two strings
    if (strcmp(str1, str2) < 0) {
        printf("Alphabetical order: %s, %s\n", str1, str2);
    } else if (strcmp(str1, str2) > 0) {
        printf("Alphabetical order: %s, %s\n", str2, str1);
    } else {
        printf("Both strings are equal: %s\n", str1);
    }

    return 0;
} 

Output:

Alphabetical order: apple, banana

Explanation:

  • Two strings, "apple" and "banana", are defined.
  • strcmp() compares str1 and str2 to determine their lexicographical order.
  • The program then prints the two strings in alphabetical order, showing how strcmp() helps order strings.

C Programming Code Editor:

Previous C Programming: C strchr()
Next C Programming:C strncmp()



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/c-programming/string/c-strcmp.php