C Programming

How to Compare Two Strings and Returns a Larger One in C Programming

Comparing two strings in C Programming is a common task that allows developers to determine the order or equality of the two string variables. However, the standard comparison operators (<, >, ==) are not directly used to compare strings in C programming. To address this, the programmers have to implement their own function or use the built-in one to compare strings based on specific criteria.

This article is a detailed description of how to compare two strings and returns a larger one in C Programming

Comparing Two Strings and Returns a Larger One in C Programming

In C Programming, you can compare the strings and return the larger ones.

1: Using Loops

To determine which string is larger, we need to compare their individual characters one by one based on the ASCII value. To run over both strings and compare each character at the same index, we may use a loop. If one string has a larger ASCII value at a specific index than the other string, we can conclude that it is the larger string. If all characters are equal, we can return either string, as both are equal in size.

#include <stdio.h>

#include <string.h>

 

char* max(const char* string1, const char* string2) {

    int len_1 = strlen(string1);

    int len_2 = strlen(string2);

    int len = (len_1 < len_2) ? len_2 : len_1;

    for (int i = 0; i < len; i++) {

        if (string1[i] > string2[i]) {

            return (char*)string1;

        }

        else if (string1[i] < string2[i]) {

            return (char*)string2;

        }

    }

    return (char*)string1;

}

int main() {

    const char* str1 = "Linux";

    const char* str2 = "Hint";

    char* largest = max(str1, str2);
   
    printf("The larger string is: %s\n", largest);

    return 0;

}

The above C program defines the function max(), which returns the longer between the two C strings (string1 and string2) as input arguments. The code calculates the length of each string using the strlen() function before utilizing a loop to compare the characters in each string.

The function returns the strings according to the lexicographical order. If string1’s ASCII value is larger than string2’s, string1 will be returned; if not, string2. The main function executes the max function with the strings “Linux” and “Hint“, saves the output in a char* pointer largest, and then uses printf() to output the longer string.

2: strcmp() Function

Another approach is to use the built-in strcmp() function, which compares two C strings and returns a value indicating their relative lexicographic order. 

#include <stdio.h>

#include <string.h>

 

char* max(const char* str1, const char* str2) {

    int res = strcmp(str1, str2);

    if (res > 0) {

        return (char*)str1;

    }

    else {

        return (char*)str2;

    }

}

int main() {

    const char* str1 = "Linux";

    const char* str2 = "Hint";

    char* largest = max(str1, str2);

    printf("The larger string is: %s\n", largest);

    return 0;

}

In this C program, the function max() is defined. It accepts two C strings, str1, and str2, as input arguments and uses the strcmp() function to return the greater of the two strings. The strings are compared according to the ASCII values. The larger string is returned as a pointer by the max function after verifying the strcmp() result.

The method returns str1 if str1 is bigger than str2. Otherwise, str2 is returned. With the strings “Linux” and “Hint” as input, the main function uses the max() function. The result is saved in a char* pointer largest, and the longer string is then printed using printf().

3: Using strlen() Function

Two strings can also be compared depending on their length irrespective of their ASCII values. We can use the strlen() function for this purpose. Look at a code example.

#include <stdio.h>

#include <string.h>


int main() {

    char str1[] = "Linux";

    char str2[] = "Hint";

    int length1 = strlen(str1);

    int length2 = strlen(str2);

    if (length1 > length2) {

        printf("String 1 is longer than String 2\n");

    } else if (length1 < length2) {

        printf("String 1 is shorter than String 2\n");

    } else {

        printf("String 1 and String 2 have the same length\n");

    }



return 0;

}

In this code, we have two strings, str1 and str2. We calculate their lengths using the strlen() function and store the results in length1 and length2 variables. The relevant message is then printed based on the comparison of the lengths using conditional statements.

Conclusion

The function that has two C string parameters and returns the larger one is defined in the above article. It can either be done using custom loops, or built-in functions like strcmp() and strlen(). Whether we are sorting, parsing, or manipulating strings, this function can help us achieve our programming goals with ease and efficiency.

About the author

Hiba Shafqat

I am a Computer Science student and a committed technical writer by choice. It is a great pleasure to share my knowledge with the world in which I have academic expertise.