Open In App

C Program to Compare Two Strings Lexicographically

Last Updated : 28 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In C, lexicographic comparison refers to comparing each character of one string to the character of other string at same position based on their alphabetical order, just like how words are arranged in a dictionary. In this article, we will learn how to compare two strings lexicographically using the C program.

The most straightforward method to compare two strings lexicographically is by using strcmp() function. Let’s take a look at an example:

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

int main() {
    char s1[] = "Geeks";
    char s2[] = "gfg";

    // Compare the two strings lexicographically using strcmp()
    int res = strcmp(s1, s2);

    if (res < 0)
        printf("\"%s\" is smaller than \"%s\".\n", s1, s2);
    else if (res > 0)
        printf("\"%s\" is greater than \"%s\".\n", s1, s2);
    else
        printf("\"%s\" is equal to \"%s\".\n", s1, s2);

    return 0;
}

Output
"Geeks" is lexicographically smaller than "gfg".

Explanation: The strncmp() function returns 0 if two strings are equal. If s1 is greater than s2, it returns positive value. If s1 is less than s2, it returns negative value.

There are also a few other methods in C to compare two strings lexicographically. Some of them are as follows:

Using strncmp()

The strncmp() compares first n characters of the given two strings in the same ways as strcmp() function.

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

int main() {
    char s1[] = "Geeks";
  	int l1 = strlen(s1);
    char s2[] = "gfg";
  	int l2 = strlen(s2);

    // Compare the two strings lexicographically using strncmp()
  	int minl = l1 < l2 ? l1 : l2;
    int res = strncmp(s1, s2, minl);

    if (res < 0)
        printf("\"%s\" is smaller than\"%s\".\n", s1, s2);
    else if (res > 0)
        printf("\"%s\" is greater than \"%s\".\n", s1, s2);
    else
        printf("\"%s\" is equal to \"%s\".\n", s1, s2);

    return 0;
}

Output
"Geeks" is smaller than"gfg".

Explanation: We took the length of the smaller string as the characters to be compared.

Using a Loop

This method compares characters of both strings in a loop until a mismatch is found or one string ends. If one string is shorter but matches entirely, it is smaller. If no mismatch occurs and lengths are equal, the strings are equal.

C
#include <stdio.h>

int strCmp(char *s1, char *s2) {
  
    // Compare until characters are equal
    while (*s1 && (*s1 == *s2)) {  
        s1++;
        s2++;
    }
  
    // Return the difference between
  	// first unmatched characters
    return (*s1 - *s2);  
}

int main() {
    char s1[] = "Hello";
    char s2[] = "Geeks";

    // Compare the two strings
    int result = strCmp(s1, s2);

    if (result < 0)
        printf("\"%s\" smaller than \"%s\".\n", s1, s2);
    else if (result > 0)
        printf("\"%s\" greater than \"%s\".\n", s1, s2);
    else
        printf("\"%s\" equal to \"%s\".\n", s1, s2);

    return 0;
}

Output
"Hello" lexicographically greater than "Geeks".


Next Article
Article Tags :

Similar Reads