What is String Comparison in C
String comparison is the process of comparing two strings (sequences of characters) to determine if they are equal, or if one is greater or less than the other. The comparison is typically based on the Unicode values of the characters in the strings, so that a string that comes first in a dictionary or alphabetical ordering is considered "less" than one that comes later. The result of a string comparison is usually represented as either equal (0), less than (-1), or greater than (1). This can be useful in various programming applications, such as sorting and searching, or for checking user input to see if it matches a predefined string.
There are several methods for comparing strings in different programming languages:
- Built-in functions: Many programming languages have built-in functions for string comparison, such as strcmp() in C, strcoll() in C++, or strcmp() in Python.
- Operators: Some programming languages have operators for string comparison, such as the equality (==) and inequality (!=) operators in many languages.
- Custom functions: You can write your own custom functions to compare strings by iterating over the characters in the strings and checking their values.
- Regular expressions: Regular expressions can be used to compare strings in a more sophisticated way, matching patterns in the strings rather than simple character-by-character comparisons.
- Library functions: Some programming languages have libraries that provide additional functions for string comparison, such as the StringUtils class in Java or the stringr package in R.
String Comparison in C
In the C programming language, string comparison is the process of comparing two strings (i.e. arrays of characters) to determine if they are equal or not. The standard function for string comparison in C is strcmp(). The function takes two string parameters as input and returns 0 if both strings are equal, a negative value if the first string is lexicographically less than the second, and a positive value if the first string is lexicographically greater than the second.
In C language, there are several methods for string comparison, including:
1. strcmp(): This is the standard library function for string comparison in C. It takes two strings as arguments and compares them character-by-character until it finds a difference or reaches to the end of one of the strings. If both strings are equal, it returns 0. If the first string is lexicographically less than the second, it returns a negative value. If the first string is lexicographically greater than the second, it returns a positive value.
Example:
#include <string.h>
#include <stdio.h>
int main()
{
char str1[10] = "Hello";
char str2[10] = "World";
int result = strcmp(str1, str2);
if (result == 0)
printf("Strings are equal.\n");
else if (result < 0)
printf("str1 is lexicographically lesser than str2.\n");
else
printf("str1 is lexicographically greater than str2.\n");
return 0;
}
Output:
str1 is lexicographically lesser than str2.
2. Custom String Comparison: You can write your own custom function to compare strings by iterating over the characters in the strings and checking their values.
Example:
#include <stdio.h>
int compare_strings(const char *str1, const char *str2)
{
while (*str1 != '\0' && *str2 != '\0')
{
if (*str1 < *str2)
return -1;
else if (*str1 > *str2)
return 1;
str1++;
str2++;
}
if (*str1 == '\0' && *str2 == '\0')
return 0;
else if (*str1 == '\0')
return -1;
else
return 1;
}
int main()
{
char str1[10] = "Hello";
char str2[10] = "World";
int result = compare_strings(str1, str2);
if (result == 0)
printf("Strings are equal.\n");
else if (result < 0)
printf("str1 is lexicographically lesser than str2.\n");
else
printf("str1 is lexicographically greater than str2.\n");
return 0;
}
Output:
str1 is lexicographically lesser than str2.
3. strncmp(): This function is similar to strcmp(), but only compares the first n characters of the strings. This can be useful if you only want to compare a certain prefix of the strings, or if you want to avoid buffer overflow issues.
Example:
#include <string.h>
#include <stdio.h>
int main()
{
char str1[10] = "Hello";
char str2[10] = "World";
int result = strncmp(str1, str2, 4);
if (result == 0)
printf("First 4 characters are equal.\n");
else if (result < 0)
printf("str1 is lexicographically lesser than str2.\n");
else
printf("str1 is lexicographically greater than str2.\n");
}
Output:
str1 is lexicographically lesser than str2.
4. Using Recursion in C: In C language, string comparison can also be performed using recursion. This involves dividing the problem of comparing two strings into smaller subproblems, by comparing the first characters of each string, and then recursively calling the function on the remaining characters in each string.
Example:
#include <stdio.h>
int compare_strings(const char *str1, const char *str2)
{
if (*str1 == '\0' && *str2 == '\0')
return 0;
else if (*str1 == '\0')
return -1;
else if (*str2 == '\0')
return 1;
if (*str1 < *str2)
return -1;
else if (*str1 > *str2)
return 1;
else
return compare_strings(str1 + 1, str2 + 1);
}
int main()
{
char str1[10] = "Hello";
char str2[10] = "World";
int result = compare_strings(str1, str2);
if (result == 0)
printf("Strings are equal.\n");
else if (result < 0)
printf("str1 is lexicographically lesser than str2.\n");
else
printf("str1 is lexicographically greater than str2.\n");
return 0;
}
Output:
str1 is lexicographically lesser than str2.