C strcoll() function
C strcoll() function - string comparison using collating information
The strcoll() function is used to compare two given strings using the collating sequence that is specified by the program's locale.
Why and when to use memchr()
- Efficient Byte Search: memchr() is useful when you need to quickly locate the first occurrence of a specified byte within a block of memory.
- Binary Data Handling: It can search through binary data or non-terminated character arrays, making it ideal for low-level operations on raw data.
- Non-Terminated Arrays: Unlike string functions like strchr(), memchr() doesn’t require a null-terminated string, so it’s great for handling arrays with arbitrary content.
Syntax:
int strcoll(const char *str1, const char *str2)
Parameters:
Name | Description | Required /Optional |
---|---|---|
str1 | First string to compare. | Required |
str2 | Second string to compare. | Required |
Return value from strcoll() function:
Name | Value |
---|---|
string1 is less than string2 | < 0 |
string1 is identical to string2 | 0 |
string1 is greater than string2 | >0 |
Examples: strcoll() function
Example 1: Compares two strings based on locale-specific rules.
This example demonstrates the strcoll() function, which compares two strings (str1 and str2) based on locale-specific rules. Here, setlocale() is set to es_US.UTF-8 to ensure locale-based comparison. The function then compares str1 and str2, printing whether str1 is less than, equal to, or greater than str2.
Code:
#include <stdio.h>
#include <string.h>
#include <locale.h>
int main()
{
char str1[] = "stdio";
char str2[] = "studio";
setlocale( LC_COLLATE, "es_US.UTF-8" );
int result =strcoll( str1, str2 );
if( result < 0 )
printf( "\nFirst is less than second.\n" );
else if( result == 0 )
printf( "\nFirst is equal to second.\n" );
else
printf( "\nFirst is greater than second.\n" );
return 0;
}
Output:
First is less than second.
Example 2: Finding a specific character in an array of characters
Code:
#include <stdio.h>
#include <string.h>
int main() {
// Define an array of characters
char arr[] = "C Programming Language";
// Character to search for
char target = 'g';
// Use memchr to find the first occurrence of 'g'
char *result = (char *)memchr(arr, target, sizeof(arr));
// Check if 'g' was found and print its position
if (result != NULL) {
printf("Character '%c' found at position %ld.\n", target, result - arr);
} else {
printf("Character '%c' not found.\n", target);
}
return 0;
}
Output:
Character 'g' found at position 5.
Explanation:
- This example finds the first occurrence of the character 'g' in the arr array using memchr() and prints its position if found.
Example 3: Searching for a byte in a binary data array
Code:
#include <stdio.h>
#include <string.h>
int main() {
// Define an array of integers (binary data)
int data[] = {0x12345678, 0x9abcdef0, 0x13579bdf, 0x2468ace0};
// Target byte to search for
char target_byte = 0xf0;
// Calculate the data array size in bytes
size_t data_size = sizeof(data);
// Use memchr to search for the target byte
char *result = (char *)memchr(data, target_byte, data_size);
// Check if the byte was found and print its offset position
if (result != NULL) {
printf("Byte 0x%x found at byte offset %ld.\n", target_byte, result - (char *)data);
} else {
printf("Byte 0x%x not found in the binary data.\n", target_byte);
}
return 0;
}
Output:
Byte 0xfffffff0 found at byte offset 4.
Explanation:
- Here, memchr() is used to find the first occurrence of the byte 0xf0 in an array of integers. The result is displayed with its byte offset in the array if found.
C Programming Code Editor:
Previous C Programming: C strncmp()
Next C Programming:C strcpy()
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/c-programming/string/c-strcoll.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics