strcmp(): string comparison

We can compare stings by using strcmp() string function.
strcmp(string1, string2);
string1, string2 : Two Input strings for camparison.
Output
  • 0 : If both strings are equal.
  • 1 ( or greater than 1 ) : If length of string1 > length of string2 .
  • -1 ( or less than -1 ) : If length of string1 < length of string2 .
We will use this and here is one example.
#include <stdio.h>
#include <string.h>
int main(void){
   char str1[]="plus2";
   char str2[]="plus2net";
   char str3[]="plus2net";
   int x,y,z;
   x=strcmp(str1,str2);
   y=strcmp(str2,str1);
   z=strcmp(str2,str3);
   printf("Output: %d , %d,%d",x,y,z);
   return 0;
}
The output of above code is here
-1,1,0
x =-1 because length of str1 is less than length of str2.
y = 1 because length of str2 is greater than length of str1.
z = 0 because length of str2 is equal to length of str3.

strcmp() is case sensitive string comparison so the output of below code is 1 ( not 0 )
#include <stdio.h>
#include <string.h>
int main(void){
   char str2[]="plus2net";
   char str3[]="Plus2net"; // Watch the first char P 
   int z;
   z=strcmp(str2,str3);
   printf("Output: %d",z);
   return 0;
}
Output
1
If the string matches then you will get 0 as output , but if strings are not matching you may get more than 1 or less than -1 as output ( based on difference in string1 and string2 ) depending upon your Operating system.

Example : In place of -1 , you may get -32 or in place of +1 , you may get 55

Example 1: Comparing Two Strings Using strcmp()

This example demonstrates how to use strcmp() to compare two strings lexicographically.

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

int main() {
    char str1[] = "apple";
    char str2[] = "banana";
    
    int result = strcmp(str1, str2);
    
    if (result == 0) {
        printf("The strings are equal.\n");
    } else if (result > 0) {
        printf("String 1 is greater than String 2.\n");
    } else {
        printf("String 1 is less than String 2.\n");
    }
    
    return 0;
}
Output:
String 1 is less than String 2.
---

Example 2: Checking If Two Strings Are Equal

This example uses strcmp() to check whether two strings are equal.

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

int main() {
    char str1[] = "Hello";
    char str2[] = "Hello";
    
    if (strcmp(str1, str2) == 0) {
        printf("The strings are equal.\n");
    } else {
        printf("The strings are not equal.\n");
    }
    
    return 0;
}
Output:
The strings are equal.
---

Example 3: Comparing Strings with User Input

This example allows the user to input two strings and compare them using strcmp().

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

int main() {
    char str1[100], str2[100];
    
    printf("Enter the first string: ");
    fgets(str1, sizeof(str1), stdin);
    str1[strcspn(str1, "\n")] = '\0';  // Remove newline character
    
    printf("Enter the second string: ");
    fgets(str2, sizeof(str2), stdin);
    str2[strcspn(str2, "\n")] = '\0';  // Remove newline character
    
    if (strcmp(str1, str2) == 0) {
        printf("The strings are equal.\n");
    } else {
        printf("The strings are not equal.\n");
    }
    
    return 0;
}
Output:
Enter the first string: Hello
Enter the second string: World
The strings are not equal.


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com






    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer