本篇介绍string.h头文件的API的学习之复制函数,连接函数,比较函数。<string.h>头文件定义了一个宏,并声明了一个类型以及多个函数,用于操作字符类型数组和其它被视为字符类型数组的对象,具体如下:
类型(type):
size_t | 表示sizeof运算符运算结果的无符号整数类型。 |
宏(macro):
NULL | 表示空指针常量的宏。 |
函数(function):
复制函数:
memcpy | 复制内存区域字符序列的函数。 |
memmove | 移动内存区域字符序列的函数。 |
strcpy | 复制字符串的函数。 |
strncpy | 从字符串中复制限定数量字符的函数。 |
连接函数:
strcat | 连接字符串的函数。 |
strncat | 向字符串添加限定数量字符的函数。 |
比较函数:
memcmp | 比较内存区域字符序列的函数。 |
strcmp | 比较字符串的函数。 |
strcoll | 比较字符串的函数。 |
strncmp | 比较字符串前n个字符的函数。 |
strxfrm | 转换字符串的函数。 |
示例代码:
#include <stdio.h>
#include <string.h>
#include <locale.h>
#pragma warning(disable:4996)
void compareString(const char *strOne, const char *strTwo)
{
int value = strcoll(strOne, strTwo);
if (value > 0)
puts("strOne is greater than strTwo.");
else if (value < 0)
puts("strOne is less than strTwo.");
else
puts("strOne is equal to strTwo.");
}
//输出字符串比较结果的函数。
void func(int value, const char *s1, const char *s2)
{
if (value > 0)
printf("%s is greater than %s.\n", s1, s2);
else if (value < 0)
printf("%s is less than %s.\n", s1, s2);
else
printf("%s is equal to %s.\n", s1, s2);
}
int main(void)
{
//memcpy 复制内存区域字符序列的函数。
const char source[] = "All for one, one for all.";
size_t length = strlen(source) + 1;
char destination[30];
memcpy(destination, source, length);
for (size_t i = 0; i < length; ++i)
printf("%c", destination[i]);
printf("\n");
//memmove 移动内存区域字符序列的函数。
char str2[] = "Chen xiaoer";
memmove(str2, (str2 + 5), (strlen(str2) - 5 + 1));
puts(str2);
//strcpy 复制字符串的函数。
const char source3[] = "Time is money.";
size_t length3 = strlen(source3) + 1;
char destination3[20];
strcpy(destination3, source3);
puts(destination3);
//strncpy 从字符串中复制限定数量字符的函数。
const char source4[] = { 'N', 'i', '\0', 'H', 'a', 'o', '\0' };
char destination4[10];
strncpy(destination4, source4, 5);
for (int i = 0; i < 5; ++i)
{
if (destination4[i] == 0)
puts("NUL");
else
printf("%c\n", destination4[i]);
}
printf("\n");
//strcat 连接字符串的函数。
char strOne[12] = "Hello ";
const char strTwo[] = "world";
strcat(strOne, strTwo);
puts(strOne);
//strncat 向字符串添加限定数量字符的函数。
char strOne6[20] = "Hello ";
const char strTwo6[] = "Tom and Jack";
strncat(strOne6, strTwo6, 6);
puts(strOne6);
//memcmp 比较内存区域字符序列的函数。
const char strOne7[] = "string";
const char strTwo7[] = "strong";
int value = memcmp(strOne7, strTwo7, 3);
if (value > 0)
puts("strOne is greater than strTwo.");
else if (value < 0)
puts("strOne is less than strTwo.");
else
puts("strOne is equal to strTwo.");
//strcmp 比较字符串的函数。
const char strOne8[] = "string";
const char strTwo8[] = "strong";
int value8 = strcmp(strOne8, strTwo8);
if (value8 > 0)
puts("strOne is greater than strTwo.");
else if (value8 < 0)
puts("strOne is less than strTwo.");
else
puts("strOne is equal to strTwo.");
//strcoll 比较字符串的函数。
const char strOne9[] = "string";
const char strTwo9[] = "String";
printf("Current locale: %s\n", setlocale(LC_COLLATE, NULL));
compareString(strOne9, strTwo9);
setlocale(LC_COLLATE, "");
printf("Current locale: %s\n", setlocale(LC_COLLATE, NULL));
compareString(strOne9, strTwo9);
//strncmp 比较字符串前n个字符的函数。
const char *name[4] = { "Geng bijun","Zhang san","Li si","Geng feng" };
//找出所有姓Geng的人。
for (int i = 0; i < 4; ++i)
{
if (strncmp(name[i], "Geng", 4) == 0)
puts(name[i]);
}
//strxfrm 转换字符串的函数。
setlocale(LC_COLLATE, "");
const char s1[] = "string";
const char s2[] = "String";
//size_t __cdecl strxfrm(
// _Out_writes_opt_(_MaxCount) _Post_maybez_ char* _Destination,
// _In_z_ char const* _Source,
// _In_ _In_range_(<= , _CRT_INT_MAX) size_t _MaxCount
//);
//char t1[1+strxfrm(NULL, s1, 0)]; //存储s1转换后的字符串。
//char t2[1+strxfrm(NULL, s2, 0)]; //存储s2转换后的字符串。
int rs1 = strxfrm(NULL, s1, 0);//16
int rs2 = strxfrm(NULL, s2, 0);//17
printf("rs1 = %d\trs2 = %d\n", rs1, rs2);
char t1[17]; //存储s1转换后的字符串。
char t2[18]; //存储s2转换后的字符串。
//int value;
/*转换前。*/
puts("Before transformation:");
/*使用strcmp函数比较源字符串。*/
value = strcmp(s1, s2);
printf("strcmp: ");
func(value, s1, s2);
/*使用strcoll函数比较源字符串。*/
value = strcoll(s1, s2);
printf("strcoll: ");
func(value, s1, s2);
puts("");
/*转换字符串。*/
strxfrm(t1, s1, sizeof(t1));
strxfrm(t2, s2, sizeof(t2));
/*转换后。*/
puts("After transformation:");
/*使用strcmp函数比较转换后字符串。*/
value = strcmp(t1, t2);
printf("strcmp: ");
func(value, s1, s2);
return 0;
}
运行结果:
参考:
https://www.standards.wiki/c/c_appendix.html
https://zh.cppreference.com/w/cpp/header/cstring
https://cplusplus.com/reference/cstring/