Open In App
Related Articles

strcoll() in C/C++

Improve Article
Improve
Save Article
Save
Like Article
Like

strcoll() is a built-in library function and is declared in <string.h> header file. This function compares the string pointed to by str1 with the one pointed by str2.The strcoll() function performs the comparison based on the rules of the current locale’s LC_COLLATE category. 

Syntax:

int strcoll(const char *str1, const char *str2)

Parameters: Function strcoll() takes two strings as parameters and returns an integer value.

Value                   Meaning
less than zero          str1 is less than str2
zero                    str1 is equal to str2
greater than zero       str1 is greater than str2

1. less than zero : When str1 is less than str2 

C++




// C++ program to illustrate strcoll()
 
#include <iostream>
#include <cstring>
using namespace std;
 
int main()
{
    char str1[13];
    char str2[13];
    int ret;
 
    strcpy(str1, "GEEKSFORGEEKS");
    strcpy(str2, "geeksforgeeks");
 
    ret = strcoll(str1, str2);
 
    if (ret > 0) {
        cout << "str1 is greater than str2";
    } else if (ret < 0) {
        cout << "str1 is lesser than str2";
    } else {
        cout << "str1 is equal to str2";
    }
 
    return 0;
}
 
// This code is contributed by sarajadhav12052009


C




// C program to illustrate strcoll()
#include <stdio.h>
#include <string.h>
 
int main()
{
    char str1[13];
    char str2[13];
    int ret;
 
    strcpy(str1, "GEEKSFORGEEKS");
    strcpy(str2, "geeksforgeeks");
 
    ret = strcoll(str1, str2);
 
    if (ret > 0) {
        printf("str1 is greater than str2");
    } else if (ret < 0) {
        printf("str1 is lesser than str2");
    } else {
        printf("str1 is equal to str2");
    }
 
    return (0);
}


Output:

str1 is lesser than str2

2. greater than zero :when str1 is greater than str2 

C++




// C++ program to illustrate strcoll()
 
#include <iostream>
#include <cstring>
using namespace std;
 
int main()
{
    char str1[13];
    char str2[13];
    int ret;
 
    strcpy(str1, "geeksforgeeks");
    strcpy(str2, "GEEKSFORGEEKS");
 
    ret = strcoll(str1, str2);
 
    if (ret > 0) {
        cout << "str1 is greater than str2";
    } else if (ret < 0) {
        cout << "str1 is lesser than str2";
    } else {
        cout << "str1 is equal to str2";
    }
 
    return 0;
}
 
// This code is contributed by sarajadhav12052009


C




// C program to illustrate strcoll()
#include <stdio.h>
#include <string.h>
 
int main()
{
    char str1[13];
    char str2[13];
    int ret;
 
    strcpy(str1, "geeksforgeeks");
    strcpy(str2, "GEEKSFORGEEKS");
 
    ret = strcoll(str1, str2);
 
    if (ret > 0) {
        printf("str1 is greater than str2");
    } else if (ret < 0) {
        printf("str1 is lesser than str2");
    } else {
        printf("str1 is equal to str2");
    }
 
    return (0);
}


Output:

str1 is greater than str2

3. Is equal to zero : when str1 is equal to str2 

C++




// C++ program to illustrate strcoll()
 
#include <iostream>
#include <cstring>
using namespace std;
 
int main()
{
    char str1[13];
    char str2[13];
    int ret;
 
    strcpy(str1, "GEEKSFORGEEKS");
    strcpy(str2, "GEEKSFORGEEKS");
 
    ret = strcoll(str1, str2);
 
    if (ret > 0) {
        cout << "str1 is greater than str2";
    } else if (ret < 0) {
        cout << "str1 is lesser than str2";
    } else {
        cout << "str1 is equal to str2";
    }
 
    return 0;
}
 
// This code is contributed by sarajadhav12052009


C




// C program to illustrate strcoll()
#include <stdio.h>
#include <string.h>
 
int main()
{
    char str1[13];
    char str2[13];
    int ret;
 
    strcpy(str1, "GEEKSFORGEEKS");
    strcpy(str2, "GEEKSFORGEEKS");
 
    ret = strcoll(str1, str2);
 
    if (ret > 0) {
        printf("str1 is greater than str2");
    } else if (ret < 0) {
        printf("str1 is lesser than str2");
    } else {
        printf("str1 is equal to str2");
    }
 
    return (0);
}


Output:

str1 is equal to str2

Related Function : strcmp(), memcmp() 

If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 26 Jun, 2023
Like Article
Save Article
Previous
Next