Character comparison in C programming language is a fundamental part of any program. As with any programming language, there are differences between how C handles character comparison on the surface and how it does so under-lie its program logic. This is the exact method websites use to verify your passwords for consistency when you join up, plagiarism detection software work, and so on.
To check whether two strings are equal, we can either compare them character by character or use a built-in function. Let’s discuss both the methods for character comparison.
Method 1: Using Comparison Operators
When it comes to character comparison in C, the most common operation is to compare two strings with the same length, often referred to as “lexicographically” comparing. This method looks at the characters in the strings individually and starts comparing the first letter in each string. From there, the second characters are compared (if they are the same, the third characters are compared, and so on). If the position of either of any two characters is not the same, then comparison stops, with the character in the lower position (i.e. the character with a lower ASCII code) considered to be the “smaller” character. Let’s follow this method with the example shown below:
void compare(char x, char y)
{
if (x == y)
printf("Both the characters are equal\n%c and %c\n", x, y);
else
printf("%c and %c are not equal\n", x, y);
}
int main()
{
char a = 'h';
char b = 'H';
char c = 'h';
compare(a, c);
compare(b, a);
compare(c, b);
return 0;
}
The above code uses the user-defined method compare() to compare two characters to see whether they are equal. By reading two-character values from the user and comparing them, this program will output “Characters are equal” if the characters are equal and “Characters are not equal” otherwise.
Output
Method 2: Using Built-in Functions
Going beyond this basic comparison, C also contains many additional tools for dealing with character comparison. For example, the library functions strcmp() and strncmp() provide greater control over comparison, while logical operators such as ||, &&, and ! add further capabilities. These advanced methods and tools can be extremely useful in ensuring the logic of comparison is more flexible and comprehensive in C programs.
i: strcmp() Function
The strcmp() function included in the string header file of the C library is also used to compare the characters. Two strings are character-by-character compared using the strcmp() method. The first character from each string in each is compared first, then the next characters.
#include<string.h>
int main()
{
char s1[10];
char s2[10];
int value;
printf("Enter a string : ");
scanf("%s",s1);
printf("Enter the second string : ");
scanf("%s",s2);
value = strcmp(s1,s2);
if(value == 0)
printf("both strings are same");
else
printf("strings are not same");
return 0;
}
The characters s1 and s2 are specified in this code. The users are then prompted to enter the characters. After the characters have been input, the strcmp() function compares them, and the output is produced based on the result.
Output
ii: strncmp() Function
The strncmp() function is a string function used to compare two strings in C. It also determines whether the two strings are equal. The third parameter is used by the strncmp() function to restrict the comparison. It implies that you can compare the first four characters, or the first five characters, etc. instead of the entire string.
The comparison between the strings is done based on ASCII values of the characters.
This method returns the following values:
- Return value less than 0 denotes that str1 is less than str2.
- Return value > 0 denotes that str2 is smaller than str1 if it is.
- If Return value is 0, it means that str1 and str2 are equivalent.
#include <string.h>
int main() {
char str1[10];
char str2[10];
int result;
strcpy(str1, "vjjdvfvd");
strcpy(str2, "SCJHSCJS");
result = strncmp(str1, str2, 6);
if(result 0) {
printf("str2 is less than str1");
} else {
printf("str1 is equal to str2");
}
return(0);
}
This code declares the characters s1 and s2. After that, the users are requested to type the characters. Following the input of the characters, the strncmp() function compares them using the number of characters chosen for comparison, and the output is generated based on the outcome.
Output
Conclusion
The character comparison in C programming language is an intricate, multifaceted process, with different methods and tools being required depending on the type of comparison being done. Understanding exactly how each part of the process works, and what the most appropriate tools are to use, is essential to successful C programming. Two methods are defined in this article to compare characters: comparison method and in-built functions.