GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — C++ cstring strcoll() Function
Selected Reading
C++ cstring strcoll() FunctionThe C++ strcoll() function is used to compare two strings based on the locale-specific collation order. The strcoll() function is similar to the strcmp() function but the difference is that strcmp() function compares two strings based on the ASCII value of the characters whereas strcoll() function compares two strings based on the locale-specific collation order.Let's take an example, where we have a project that shows the regional data. In this project, we need to sort or group the data based on the locale-specific collation order. In this case, we can use the strcoll() SyntaxFollowing is the syntax of the strcoll() function. int strcoll(const char *str1, const char *str2); ParametersParameters of the strcoll() function are as follows −
Return ValuesThis function returns −
Example 1This example help you to understand the usage of the C++ strcoll() function. Here, we will take two strings str1 and str2 and then compare them using the strcoll() function. #include <iostream>
#include <cstring>
using namespace std;
int main() {
char str1[15];
char str2[15];
int ret;
strcpy(str1, "abcdef");
strcpy(str2, "ABCDEF");
ret = strcoll(str1, str2);
if(ret > 0) {
cout << "str1 is greater than str2"<<endl;
} else if(ret < 0) {
cout << "str1 is less than str2"<<endl;
} else {
cout << "str1 is equal to str2"<<endl;
}
return 0;
}
OutputFollowing is the output of the above C++ program − str1 is less than str2 Example 2Now, Let's take another example where we will compare output of the strcoll() function with the strcmp() function − #include <iostream>
#include <cstring>
using namespace std;
int main() {
char str1[15];
char str2[15];
int ret;
strcpy(str1, "abcdef");
strcpy(str2, "ABCDEF");
ret = strcoll(str1, str2);
if(ret > 0) {
cout << "str1 is greater than str2 using strcoll() function"<<endl;
} else if(ret < 0) {
cout << "str1 is less than str2 using strcoll() function"<<endl;
} else {
cout << "str1 is equal to str2 using strcoll() function"<<endl;
}
ret = strcmp(str1, str2);
if(ret > 0) {
cout << "str1 is greater than str2 using strcmp() function"<<endl;
} else if(ret < 0) {
cout << "str1 is less than str2 using strcmp() function"<<endl;
} else {
cout << "str1 is equal to str2 using strcmp() function"<<endl;
}
return 0;
}
OutputFollowing is the output of the above C++ program − str1 is less than str2 using strcoll() function str1 is greater than str2 using strcmp() function You can see that the output of the strcoll() function is different from the output of the strcmp() function. The strcoll() function compares two strings based on the locale-specific collation order whereas the strcmp() function compares two strings based on the ASCII value of the characters. Advertisements
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |