How to Properly Compare Strings in C

When working with strings in C, it is important to properly compare them to ensure accurate results. In this article, we will discuss the correct way to compare strings in C and address the issue mentioned in the problem description.

Using the strcmp() function

C provides the strcmp() function to compare two strings. It compares the contents of two strings and returns an integer value specifying the result of the comparison. The function takes two arguments, the strings to be compared.

Here is an example of using the strcmp() function to compare two strings:


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

            int main() {
                char str1[] = "hello";
                char str2[] = "world";

                int result = strcmp(str1, str2);

                if (result == 0) {
                    printf("The strings are equal");
                } else if (result < 0) {
                    printf("String 1 is less than string 2");
                } else {
                    printf("String 1 is greater than string 2");
                }

                return 0;
            }
        

The output of this program will be "String 1 is less than string 2" because in lexicographical order, "hello" comes before "world".

Comparing strings using pointers

Pointers can also be used to compare strings in C. By comparing the characters in memory, we can determine if two strings are equal. Here is an example:


            #include <stdio.h>

            int main() {
                char str1[] = "hello";
                char str2[] = "world";

                int i = 0;

                while (str1[i] == str2[i]) {
                    if (str1[i] == '\0') {
                        printf("The strings are equal");
                        break;
                    }
                    i++;
                }

                if (str1[i] != str2[i]) {
                    printf("The strings are not equal");
                }

                return 0;
            }
        

This program compares the characters of the two strings using a while loop. It checks if the characters at each index are equal and continues until it finds a mismatch or reaches the end of one of the strings.

Addressing the problem in the code

In the code mentioned in the problem description, the issue lies in the comparison check != input. This comparison compares the memory addresses of the two arrays, not their contents.

To fix this issue, we need to use the strcmp() function to compare the strings. Here is an updated version of the code:


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

            int main() {
                char input[40];
                char check[40];
                printf("Hello!\nPlease enter a word or character:\n");
                gets(input);   /* obsolete function: do not use!! */
                printf("I will now repeat this until you type it back to me.\n");

                while (strcmp(check, input) != 0) {
                    printf("%s\n", input);
                    gets(check);   /* obsolete function: do not use!! */
                }

                printf("Good bye!");
                
                return 0;
            }
        

In this updated code, we use strcmp(check, input) to compare the input string with the check string. The while loop will continue until the comparison returns 0, indicating that the strings are equal.

Conclusion

Properly comparing strings in C is crucial to ensure accurate results. By using the strcmp() function or comparing strings using pointers, you can accurately determine if two strings are equal or not. Remember to avoid comparing strings using the inequality operator (!=) as it compares memory addresses, not the contents of the strings.