How to Compare Two Strings in C Programming

financierpro007@gmail.com

How to Compare Two Strings in C Programming

In C programming, strings are essential data structures that allow us to store and manipulate sequences of characters. Comparing strings is a common operation performed to determine the equality or order of two strings. In this comprehensive guide, we will explore various methods to compare strings in C, including the use of standard library functions and custom functions.

Overview of Strings in C


In C programming, strings are represented as arrays of characters, terminated by a null character (‘\0’). This null character indicates the end of the string, allowing the program to determine the string’s length. Since strings are character arrays, we can access individual characters using the array index notation.

Comparing Strings Using the strcmp Function


The C standard library provides a function called strcmp (string compare) that allows us to compare two strings lexicographically. This function returns an integer value, indicating the result of the comparison. Here’s how the strcmp function works:

a. If both strings are equal, the function returns 0.
b. If the first string is lexicographically greater than the second string, the function returns a positive value.
c. If the first string is lexicographically smaller than the second string, the function returns a negative value.

To use the strcmp function, we need to include the header file in our program. The following example demonstrates how to use strcmp to compare two strings:

c
Copy code

include

int main() {
char string1[] = “Hello”;
char string2[] = “World”;
char string3[] = “Hello”;

int result = strcmp(string1, string2);

if (result == 0) {
    printf("string1 and string2 are equal\n");
} else if (result < 0) {
    printf("string1 is lexicographically smaller than string2\n");
} else {
    printf("string1 is lexicographically greater than string2\n");
}

result = strcmp(string1, string3);

if (result == 0) {
    printf("string1 and string3 are equal\n");
} else if (result < 0) {
    printf("string1 is lexicographically smaller than string3\n");
} else {
    printf("string1 is lexicographically greater than string3\n");
}

return 0;

}


Comparing Strings Using the strncmp Function


The strncmp (string compare with a specified number of characters) function is another standard library function that allows us to compare a specified number of characters of two strings. Like strcmp, this function also returns an integer value indicating the result of the comparison.

The strncmp function is particularly useful when we want to compare a specific portion of two strings. To use the strncmp function, include the header file in the program. The following example demonstrates how to use strncmp to compare a specific number of characters of two strings:

c
Copy code

include

int main() {
char string1[] = “Hello, World!”;
char string2[] = “Hello, Universe!”;

int n = 7; // Number of characters to compare

int result = strncmp(string1, string2, n);

if (result == 0) {
    printf("The first %d characters of string1 and string2 are equal\n", n);
} else if (result < 0) {
    printf("The first %d characters of string1 are lexicographically smaller t n);
} else {
printf("The first %d characters of string1 are lexicographically greater than those of string2\n", n);
}

kotlin
Copy code
return 0;
}

vbnet
Copy code

4. Comparing Strings Using a Custom Function

In addition to the standard library functions, we can also create our own custom function to compare strings. This can be helpful when we need to implement a specific comparison logic or want to avoid using the standard library functions. The following example demonstrates how to create a custom string comparison function:

```c
#include <stdio.h>

int custom_strcmp(const char *string1, const char *string2) {
    while (*string1 && *string1 == *string2) {
        string1++;
        string2++;
    }

    return *string1 - *string2;
}

int main() {
    char string1[] = "Hello";
    char string2[] = "World";
    char string3[] = "Hello";

    int result = custom_strcmp(string1, string2);

    if (result == 0) {
        printf("string1 and string2 are equal\n");
    } else if (result < 0) {
        printf("string1 is lexicographically smaller than string2\n");
    } else {
        printf("string1 is lexicographically greater than string2\n");
    }

    result = custom_strcmp(string1, string3);

    if (result == 0) {
        printf("string1 and string3 are equal\n");
    } else if (result < 0) {
        printf("string1 is lexicographically smaller than string3\n");
    } else {
        printf("string1 is lexicographically greater than string3\n");
    }

    return 0;
}

In this example, the custom_strcmp function iterates through both strings, comparing each character one by one. When it encounters a mismatch or reaches the end of the strings, it returns the difference between the ASCII values of the corresponding characters. This ensures that the function returns a value that follows the same rules as the strcmp function.

Conclusion

Comparing strings is a fundamental operation in C programming, and there are multiple ways to perform string comparisons. The standard library provides the strcmp and strncmp functions for lexicographical comparisons, which are widely used and efficient. However, we can also create custom functions to compare strings according to specific requirements. Understanding the different methods of string comparison is essential for any C programmer, as it allows us to choose the most appropriate approach based on the particular needs of a given task.