String comparison in Data Structure using C

In C, you can use the `strcmp()` function from the `string.h` library to compare two strings. The function returns an integer value that indicates the relationship between the two strings. Here is an example:

#include <stdio.h>
#include <string.h>

int main() {
    char str1[100] = "Hello";
    char str2[100] = "Hello";

    int result = strcmp(str1, str2); // Compare str1 and str2

    if (result == 0) {
        printf("The strings are equal");
    } else {
        printf("The strings are not equal");
    }

    return 0;
}
```

In this example, the `strcmp()` function is used to compare `str1` and `str2`. If the two strings are equal, the function returns 0. If `str1` is greater than `str2`, the function returns a positive value. If `str2` is greater than `str1`, the function returns a negative value.

In the example, we use an `if` statement to check the result of `strcmp()`. If the result is 0, the strings are equal, and we print a message to that effect. Otherwise, we print a message indicating that the strings are not equal.



About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext