How to Compare 2 Strings in C
Comparing two strings in C is a fundamental operation that is essential for many programming tasks. Whether you’re checking for equality, validating user input, or implementing complex algorithms, the ability to compare strings is crucial. In this article, we will explore various methods to compare two strings in C and understand their nuances.
1. Using the ‘==’ Operator
The simplest way to compare two strings in C is by using the ‘==’ operator. This operator checks whether the two strings are equal in both value and content. Here’s an example:
“`csharp
string str1 = “Hello”;
string str2 = “Hello”;
string str3 = “World”;
Console.WriteLine(str1 == str2); // Output: True
Console.WriteLine(str1 == str3); // Output: False
“`
In the above example, `str1` and `str2` are equal, so the output is `True`. However, `str1` and `str3` are not equal, so the output is `False`.
2. Using the ‘Equals’ Method
The `Equals` method is another way to compare two strings in C. It is similar to the ‘==’ operator, but it allows you to specify a comparison type. The `StringComparison` enumeration defines various comparison types, such as `Ordinal`, `OrdinalIgnoreCase`, and `CurrentCulture`.
“`csharp
string str1 = “Hello”;
string str2 = “hello”;
string str3 = “World”;
Console.WriteLine(str1.Equals(str2, StringComparison.Ordinal)); // Output: False
Console.WriteLine(str1.Equals(str2, StringComparison.OrdinalIgnoreCase)); // Output: True
Console.WriteLine(str1.Equals(str3, StringComparison.OrdinalIgnoreCase)); // Output: False
“`
In the above example, `str1` and `str2` are not equal when using `StringComparison.Ordinal` because the comparison is case-sensitive. However, when using `StringComparison.OrdinalIgnoreCase`, the comparison is case-insensitive, and the output is `True`.
3. Using the ‘CompareTo’ Method
The `CompareTo` method is used to compare two strings lexicographically. It returns an integer that indicates the relationship between the two strings. If the strings are equal, it returns 0. If the first string is less than the second string, it returns a negative value. If the first string is greater than the second string, it returns a positive value.
“`csharp
string str1 = “Hello”;
string str2 = “hello”;
string str3 = “World”;
Console.WriteLine(str1.CompareTo(str2)); // Output: 32
Console.WriteLine(str1.CompareTo(str3)); // Output: -15
“`
In the above example, `str1` is greater than `str2`, so the output is 32. Similarly, `str1` is less than `str3`, so the output is -15.
4. Using the ‘StartsWith’ and ‘EndsWith’ Methods
The `StartsWith` and `EndsWith` methods are useful for checking if a string starts or ends with a specific substring. These methods return a boolean value indicating whether the condition is met.
“`csharp
string str1 = “Hello World”;
string str2 = “World”;
Console.WriteLine(str1.StartsWith(str2)); // Output: True
Console.WriteLine(str1.EndsWith(str2)); // Output: True
“`
In the above example, `str1` starts with `str2`, so the output is `True`. Similarly, `str1` ends with `str2`, so the output is `True`.
Conclusion
In this article, we discussed various methods to compare two strings in C. Understanding these methods will help you choose the appropriate technique for your specific needs. Whether you’re checking for equality, validating user input, or implementing complex algorithms, the ability to compare strings is a fundamental skill in C programming.