本篇介绍string.h头文件的API的学习之搜索函数和其它函数。<string.h>头文件定义了一个宏,并声明了一个类型以及多个函数,用于操作字符类型数组和其它被视为字符类型数组的对象,具体如下:
搜索函数:
memchr | 搜索指定字符在内存区域第一次出现位置的函数。 |
strchr | 搜索指定字符在字符串中第一次出现位置的函数。 |
strcspn | 计算最大初始片段长度的函数。 |
strpbrk | 搜索字符在字符串中第一次出现位置的函数。 |
strrchr | 搜索指定字符在字符串中最后一次出现位置的函数。 |
strspn | 计算最大初始片段长度的函数。 |
strstr | 搜索子字符串在字符串中第一次出现位置的函数。 |
strtok | 拆分字符串的函数。 |
其它函数:
memset | 填充内存区域的函数。 |
strerror | 获取出错信息的函数。 |
strlen | 计算字符串长度的函数。 |
示例代码:
#include <stdio.h>
#include <string.h>
#include <locale.h>
#include <errno.h>
#include <math.h>
#pragma warning(disable:4996)
int main(void)
{
//memchr 搜索指定字符在内存区域第一次出现位置的函数。
const char str[] = "There is no rose without a thorn.";
const char *ptr = str;
char ch = 'o';
//输出字符串中所有字母o的位置。
puts("The position of character o: ");
while (ptr = memchr(ptr, ch, strlen(ptr)))
{
printf("%td ", (++ptr - str));
}
//strchr 搜索指定字符在字符串中第一次出现位置的函数。
const char str2[] = "There is no rose without a thorn.";
char *ptr2 = str2;
char ch2 = 'o';
//输出字符串中所有字母o的位置。
printf("The position of character %c:\n", ch2);
while (ptr2 = strchr(ptr2, ch2))
{
printf("%td ", (++ptr2 - str2));
}
//strcspn 计算最大初始片段长度的函数。
const char str3[] = "There is no rose without a thorn.";
size_t number = strcspn(str3, "lmn");
//初始片段内容。
printf("Segment: %.*s\n", number, str3);
//初始片段长度。
printf("Length: %zu\n", number);
//strpbrk 搜索字符在字符串中第一次出现位置的函数。
const char str4[] = "There is no rose without a thorn.";
char *pch4 = strpbrk(str4, "aeiou");
printf("The position of the first occurrence: %td\n", (pch4 - str4 + 1));
//strrchr 搜索指定字符在字符串中最后一次出现位置的函数。
char str5[] = "There is no rose without a thorn.";
char *ptr5;
char ch5 = 'o';
ptrdiff_t diff5;
//输出字符串中所有字母o的位置。
printf("The position of character %c:\n", ch5);
while (ptr5 = strrchr(str5, ch5))
{
diff5 = ptr5 - str5;
printf("%td ", (diff5 + 1));
str5[diff5] = '\0';
}
//strspn 计算最大初始片段长度的函数。
const char str6[] = "You can fool all the people some of the time,\
and some of the people all the time,\
but you can not fool all the people all the time.";
const char subStr6[] = "aeiou";
const char *ptr6 = str6;
size_t count6 = 0;
size_t length6;
//统计元音字母出现的次数。
while (*ptr6 != '\0')
{
length6 = strspn(ptr6, subStr6);
if (length6 == 0)
++ptr6;
else
{
count6 += length6;
ptr6 += length6;
}
}
printf("Total %zu vowel%s.\n", count6, (count6 > 1) ? "s" : "");
//strstr 搜索子字符串在字符串中第一次出现位置的函数。
const char strOne7[] = "You can fool all the people some of the time,\
and some of the people all the time,\
but you can not fool all the people all the time.";
const char strTwo7[] = "the";
const char *ptr7 = strOne7;
int count7 = 0;
//统计单词the出现的次数。
while (ptr7 = strstr(ptr7, strTwo7))
{
++count7;
ptr7 += strlen(strTwo7);
}
printf("Total %d time%s.\n", count7, (count7 > 1) ? "s" : "");
//strtok 拆分字符串的函数。
char strOne8[] = "You can fool all the people some of the time,\
and some of the people all the time,\
but you can not fool all the people all the time.";
const char strTwo8[] = ",.\' \"?!";
char *ptr8;
int count8 = 0;
//统计句子中的单词数。
ptr8 = strtok(strOne8, strTwo8);
while (ptr8 != NULL)
{
++count8;
ptr8 = strtok(NULL, strTwo8);
}
printf("Total %d word%s.\n", count8, (count8 > 1) ? "s" : "");
//memset 填充内存区域的函数。
char str9[] = " 10000";
memset(str9, '$', 1);
puts(str9);
//strerror 获取出错信息的函数。
double a;
errno = 0;
a = sqrt(-1.0);
printf("Error: %s\n", strerror(errno));
//strlen 计算字符串长度的函数。
const char str10[] = "All for one, one for all.";
printf("Length: %zu\n", strlen(str10));
return 0;
}
运行结果:
参考:
https://www.standards.wiki/c/c_appendix.html
https://zh.cppreference.com/w/cpp/header/cstring
https://cplusplus.com/reference/cstring/