Making Comparisons Using the C strcmp Function
Comparisons are fairly common while using the C programming language. There is code used to compare if a given integer is less than, greater than or equals some other integer or input. There are also comparisons that can be used as a method of security. For example, when users enter their passwords, there is a comparison done to ensure that the username and password combinations are valid so that only those with proper credentials are able to access information. You may ask, “How could computer code provide security by simply comparing words?” There is a simple answer to that question. If you only need to compare two words you can use the C “strcmp” function.
Are you a beginner to the C programming language? Learn how to write your own code in C today!
By far the most common string comparison function is strcmp. This is because of its straightforward format. Below is a snippet of C code that shows the basic format of this function:
strcmp (const char *s1, const char *s2)
The strcmp function compares the string of s1 to that of s2 and returns a value. The function returns a 0 if the two strings are of equal value, a positive number is returned if s1 has a greater value than s2 and a negative number is returned if s1 has a smaller value than s2. While using the strcmp function, you must be careful as to which string you enter first. If you are not careful, you will return an unexpected value that can cause havoc on your code.
Learn the basics needed to program with C and more!
You want to compare words, so you may be a little confused as to why we would use a function that compares strings. While using C you should be aware that a string is an array of characters that ends with a null character (). By this definition, words as we are used to calling them, are also strings. Let’s try look at a snippet of code below that will explain strcmp a little better:
char str1[] = “tiger”; char str2[] = “tiger”; a = strcmp(str1, str2);
In this example, what do you think the value of “a” will be? If you guessed that a = 0 then you are exactly right. Both strings are set to equal “tiger” so they are equal. Now we will take a look at a little more realistic situation where this function is used:
/* Sample C program illustrating the use of strcmp function */ #include<stdio.h> #include<string.h> void main() { char str1[] = “Alphabetize”; char str2[] = “Alphabets”; int a; a = strcmp(str2, str1); if(a == 0) { printf(“The strings are equal”); } else { printf(“The strings are not equal”); } return 0; }
Udemy is the place to learn to program using C!
Before analyzing what the code will do, take a look at the header files. The header <string.h> is included along with <stdio.h>. This is because the strcmp function is defined in the <string.h> header. If this header is not included in the code, then the strcmp along with a lot of other functions will not work properly and will lead to errors when compiling.
The code above compares the two strings str1 and str2. When this code is compiled, what will be printed the screen? When the comparison is done we should expect the if-else function to print “The strings are not equal” to the screen because “Alphabetize” and “Alphabets” are not exactly equal.
In the code, you can see that “a” was declared as an integer. This is because when the two strings are compared the function returns a numerical value. You may be wondering why a numerical value is returned and what the value of “a” will be. This value will be equal to the ASCII difference of the first two characters that do not match. In the code of the examples above, the characters “i” and “s” are the first two characters that do not match. The ASCII value of i is 105 and s has a value of 115. This gives you a difference of 10 which means that the value of the string str2 has a greater value than that of str1.
Learn how to find the size of data types using C.
As you are reading over this description of the strcmp function you may be asking yourself what are some useful tasks that strcmp can perform. As mentioned in the introduction of this article, strcmp would be useful for comparing passwords. The strcmp function compares two strings and only returns a value of 0 if both strings are exactly equal. This is helpful because if an entered password does not match the password in a database used by the code then entry to that website will be denied. The code below shows how strcmp can be used for password verification.
/* Sample C program illustrating the use of strcmp function */ #include<stdio.h> #include<string.h> void main() { char userpassword[] = cisgreat; char userpasswordEntered[]; int a; printf(“Please enter your password: “); scanf(userpasswordEntered); a = strcmp(userpassword, userpasswordEntered); if (a == 0) { printf(“Password is verified”); } else { printf(“Password not valid”); } return 0; }
The code above has assigned the user’s password as “cisgreat” and makes use of the scanf function to obtain the password from a user before allowing access to sensitive material. If any combination of characters other than “cisgreat” is entered as the password the if-else function prints “Password not valid” to the screen. Even if the string “CISGREAT” was entered, the if statement would fail. This is due to the fact that although the same letters are used, the string “CISGREAT” is not seen as being equal to “cisgreat” by the compiler. This is because the ASCII code of each character is compared to that of the character in the same place in the other string. The characters “C” and “c” have ASCII codes of 67 and 99 respectively which are not equal.
Strcmp is one of the most used comparison functions of the C language. It can be used to test the equality of two simple words or strings like “cat” and “dog.” It may also be used in a much more serious manner as part of the process of password encryption.
Recommended Articles
Top courses in C# (programming language)
C# (programming language) students also learn
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.