C++ cstring strcoll() Function
Example
Compare two C-style strings with strcoll():
char str1[] = "Apple";
char str2[] = "Banana";
int result = strcoll(str1, str2);
cout << (result < 0 ? "Apple comes first" : "Banana comes first");
Definition and Usage
The strcoll() function compares two C-style strings, and returns a number that tells which string comes first.
It uses the sorting rules of the current locale (the language and country settings of the program). The default locale is "C", and then strcoll() works the same as strcmp().
The strcoll function is defined in the <cstring> header file.
Tip: You can change the locale with the setlocale() function from the <clocale> library. Which locales you can use depends on the system.
Syntax
strcoll(const char* str1, const char* str2);
Parameter Values
| Parameter | Description |
|---|---|
| str1 | Required. The first string to compare. |
| str2 | Required. The second string to compare. |
Technical Details
| Returns: | An int value: 0 if the strings are equal, a negative number if the first string comes first, and a positive number if the second string comes first. |
|---|