C Programming

Strcasecmp Function in C

The Strcasecmp functions allow the comparison between the two strings. This case comparison function is not sensitive to either lowercase or uppercase letters or the alphabets in the strings since this function first converts both the string’s characters to lowercase and then compares them. The comparison is always done between those strings that terminate at some null character, e.g. “/0”. Strcasecmp starts comparing the two different strings with the first character in both strings. This comparison terminates at a null character which indicates that the string has ended. This function returns some values as its return value after the comparison. These return values are represented as less than zero which means that the first string is smaller than the second. Equal to “0” or “0” shows that both strings are equal and more than zero which means that the first string is more/ greater than the second.

Procedure

We will talk about the methods to compare the two different strings of different sizes using the strcasecmp() method. We will explore the declaration method for this function and learn where we can make use of this function. To get hands-on with this function, we will solve some examples that use the string case comparison method.

Syntax

The method for the declaration of strcasecmp() involves the use of pointers that point towards the two strings that we want to compare with each other. We first define the return type for this function which is “integer”. Then, we name the function “strcasecmp” and define its parameters as the pointers of both strings. This declaration is represented in the next line as follows:

$ Int strcasecmp (char * string_1, char * str_2)

Example 1

This example uses the previously-mentioned explanation of the strcasecmp() function to compare the strings. To begin with the comparison method, we execute the example in the C in the Microsoft Visual Studio compiler. Our first step would be to create a new project for C. And then, we include the required libraries from the C fundamentals to either read or write the inputs and outputs in the program. Moreover, since we are dealing with the strings in this article, we also import the header file that lets us use the string-related functions in the program. We import only two of the header “.h” files following the commands:

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

Following the example of this step, we now begin with creating the program for the strcasecmp() function. To do this, we declare the two strings with the data type as “char”. We may name these strings “string_1” and “string_2”, respectively. We initialize these strings with some random values as “CHAIRS” to string_1 and “chairs” to string_2. After this assignment of the values to the strings, we define the pointers for both of these strings with the data type “char” and initialize a variable as “compare result” that has the data type “integer”.

To this variable, we assign the strcasecmp() function that has the string_1 and string_2 as its input arguments. Then, we imply the” if else” conditions to this result variable, that if the result is equal to “0”, print “strings are equal”. If the result is “less than 0”, display “string_1 is less than string_2” or “string_1 is greater than string_2” and return the value equal to 0 to the main function. We try to re-write the explanation that we mentioned for example number 1 in the form of the program using the C language in the following figure:

#include <stdio.h>
#include <strings.h>

int main(void)
{ char* string_1 = "CHAIRS";
char* string_2 = "chairs";
int compare_result;
compare_result = strcasecmp(string_1, string_2);
if (compare_result == 0)
printf("equal strings.\n");
else if (compare_result < 0)
printf(""%s" string1 is less than string2 "%s".\n", string_1, string_2);
else
printf(""%s" string1 is greater than string2"%s".\n", string_1, string_2);

return 0;
}

The results of the comparison that we made for both strings 1 and 2 came out. The output displays that the strings are equal which is the correct estimation as the strings that we compared were the same.

Example 2

In the second example, we execute a code for the comparison of the two strings that we take the values for, from the user, which means that we take the values of two strings from the user and print the result of their comparison. We import the header files for the string functions and the output display and input reading as “string. h” and “stdio. h”, respectively. To start coding, we declare the strings (two) having the data size as “50” and the data type “char”.

To initialize these strings, we take the input from the user by calling the “scanf()” method and pass the format specifier “%s” and the name of the strings. We store the comparison result of both user-defined strings in a variable “Compare_result” and impose an “if statement” that if the result is equal to zero, the “strings are equal”. Otherwise, they are “not equal”. If the statement is true, we print the “equal strings”. In case the statement gets wrong, the program prints “not equal”. The program then exits the main function by returning the zero as its return value. We attached a following code snippet for the implementation of this example:

#include <stdio.h>
#include <strings.h>

int main(void)
{
char string_1[50];
char string_2[50];
printf("enter string_1");
scanf("%s", string_1);
printf("enter string_2");
scanf("%s", string_2);
int compare_result;
compare_result = strcasecmp(string_1, string_2);
if (compare_result == 0)
printf("equal strings");
else
printf(" not equal");

return 0;
}

The two user-defined strings were “Chairs” and “TABLE”, respectively. The result of the comparison came out to be “not equal” which is correct as the two strings that were compared in the program were not equal. We can also apply the conditions for the greater than and less than for a comparison result.

Conclusion

The use of the string comparison function is very common in almost every application written in any programming language. We discussed the use of the strcasecmp() function in this article for the C language. This comparison function takes the comparison between the two strings and this function is insensitive to lowercase and uppercase letters. We implemented this string comparison function for both the strings that were defined in the program and the user-defined strings by calling the scanf() method.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.