Write an efficient function to implement strcmp() function in C. The standard strcmp() function compares the two strings and returns an integer indicating the relationship between them.

The prototype of the strcmp() is:

int strcmp(const char* X, const char* Y);

The strcmp() function returns an integer greater than, equal to, or less than zero, accordingly as the string pointed to by X is greater than, equal to, or less than the string pointed to by Y.

The function basically performs a binary comparison of both strings’ characters until they differ or until a terminating null character is reached.

C


Download  Run Code

Output:

X is greater than Y

 
The time complexity of the above solution is O(min(n, m)), where n and m are the lengths of the two strings.

 
Excercise: Implement strncmp() function in C

That’s all about strcmp() implementation in C.