- 6.5.4 字符串函数库
- include<string.h>//头文件
- include<stdio.h>
- include<reg51.h>
- include<string.h>//头文件
- include<stdio.h>
- include<reg51.h>
- include<string.h>//头文件
- include<stdio.h>
- include<reg51.h>
- include<string.h>//头文件
- include<stdio.h>
- include<reg51.h>
- include<string.h>//头文件
- include<stdio.h>
- include<reg51.h>
- include<string.h>//头文件
- include<stdio.h>
- include<reg51.h>
- include<string.h>//头文件
- include<stdio.h>
- include<reg51.h>
- include<string.h>//头文件
- include<stdio.h>
- include<reg51.h>
- include<string.h>//头文件
- include<stdio.h>
- include<reg51.h>
- include<string.h>//头文件
- include<stdio.h>
- include<reg51.h>
- include<string.h>//头文件
- include<stdio.h>
- include<reg51.h>
- include<string.h>//头文件
- include<stdio.h>
- include<reg51.h>
- include<string.h>//头文件
- include<stdio.h>
- include<reg51.h>
- include<string.h>//头文件
- include<stdio.h>
- include<reg51.h>
- include<string.h>//头文件
- include<stdio.h>
- include<reg51.h>
- include<string.h>//头文件
- include<stdio.h>
- include<reg51.h>
- include<string.h>//头文件
- include<stdio.h>
- include<reg51.h>
- include<string.h>//头文件
- include<stdio.h>
- include<reg51.h>
- include<string.h>//头文件
- include<stdio.h>
- include<reg51.h>
- include<string.h>//头文件
- include<stdio.h>
- include<reg51.h>
- include<string.h>//头文件
- include<stdio.h>
- include<reg51.h>
- include<string.h>//头文件
- include<stdio.h>
- include<reg51.h>
6.5.4 字符串函数库
字符串函数的原型声明包含在头文件STRING.H中。在C51语言中,字符串应包括两个或多个字符,字符串的结尾以空字符来表示。字符串函数通过接受指针串来对字符串进行处理。常用的字符串函数介绍如下。字符串函数库的函数如表6.9所示。
1.字符查找函数memchr
字符查找函数memchr的函数原型如下。
voidmemchr(voidsl,char val,int len);
其中,参数s1为输入字符串,val为待查找的字符,len为查找的长度范围。字符查找函数memchr用于在字符串中顺序查找字符。该函数的功能是在字符串s1中按照顺序搜索前len个字符以找出字符val,如果找到该字符则返回sl中指向val的指针,如果没有找到则返回NULL。程序示例如下。
include<string.h>//头文件
include<stdio.h>
include<reg51.h>
void main(void)//主函数
{
static char str[20]=“This is a test!”;
void*pc;
//初始化串口
pc=memchr(str,‘a',sizeof(str));//查找字符'a’
if(pc!=NULL)//找到字符
printf(“found‘a’in this string\n”);
else//未找到字符
printf(“‘a’was not found in this string\n”);
}
该程序可以在KeilµVision3编译环境中执行,运行的结果如下。
found‘a’in this string
在该程序中,首先初始化了一个字符串str,接着调用memchr函数在str中搜索字符“a”,运行的结果将使用printf函数通过串口输出。
2.指定长度的字符串比较函数memcmp
指定长度的字符串比较函数memcmp的函数原型如下。
char memcmp(voids1,voids2,int len);
其中,s1和s2为输入字符串,len为比较的长度。指定长度的字符串比较函数memcmp用于按照指定的长度比较两个字符串的大小。该函数执行的操作是逐个比较字符串sl和s2的前len个字符,如果相等则返回0;如果字符串s1大于s2,则返回一个正数;如果字符串s1小于s2,则返回一个负数。如果两个字符串的长度小于len,该函数仍将一直比较len个字符,这种情况下,有可能导致结果出错。因此,在使用时应该保证参数len不能超过最短字符串的长度。程序示例如下。
include<string.h>//头文件
include<stdio.h>
include<reg51.h>
void main(void)//主函数
{
static char str1[]=“12345abcD”;//初始化字符串
static char str2[]=“12345ABCD”;
char i;
//初始化串口
i=memcmp(str1,str2,7);//比较字符串
if(i>0)//打印比较结果
printf(“str1>str2\n”);
else if(i<0)
printf(“str1<str2\n”);
else
printf(“str1==str2\n”);
}
该程序可以在KeilµVision3编译环境中执行,运行的结果如下。
str1>str2
在该程序中,首先初始化了字符串str1和str2,接着调用memcmp函数比较这两个字符串,共比较7个字符。运行的结果将通过串口输出。
3.字符串复制函数memcpy
字符串复制函数memcpy的函数原型如下。
voidmemcpy(voiddest,void*src,int len);
其中,参数dest为目标字符串,src为源字符串,len为复制的长度。字符串复制函数memcpy用于复制指定长度的字符串。该函数执行的操作是从src所指向的字符串中复制len个字符到dest字符串中,其返回值指向目标字符串dest中的最后一个字符的指针。程序示例如下。
include<string.h>//头文件
include<stdio.h>
include<reg51.h>
void main(void)//主函数
{
static char str1[]=“12345abcD”;
static char str2[20];
char*p;
int n;
//初始化串口
n=sizeof(str);
p=memcpy(str2,str1,n);//复制字符串
printf(“str2=%s\n”,str2);//输出结果
}
该程序可以在KeilµVision3编译环境中执行,运行的结果如下。
str2=12345abcD
在该程序中,首先初始化了字符串str1,接着调用memcpy函数将字符串str1复制到空字符串str2中,最后输出字符串str2中的结果。
4.带终止字符的字符串复制函数memccpy
带终止字符的字符串复制函数memccpy的函数原型如下。
voidmemccpy(voiddest,void*src,char val,int len);
其中,参数dest为目标字符串,src为源字符串,val为终止字符,len为复制的长度。带终止字符的字符串复制函数memccpy用于复制字符串,如果遇到终止字符则停止复制。该函数执行的操作是复制字符串src中的len个字符到dest中,复制len个字符后则返回NULL。如果遇到字符val则停止复制,此时返回一个指向目标字符串dest中的下一个元素的指针。程序示例如下。
include<string.h>//头文件
include<stdio.h>
include<reg51.h>
void main(void)//主函数
{
static char str1[]=“This is a test string!”;//初始化
static char str2[15];
char*p;
//初始化串口
p=memccpy(str2,str1,‘a’,sizeof(str1));//复制字符串
printf(“str2=%s\n”,str2);//输出结果
}
该程序可以在KeilµVision3编译环境中执行,运行的结果如下。
str2=This is a
在该程序中,首先初始化字符串str1,接着调用memccpy函数将字符串str1复制到空字符串str2中,如果遇到字符“a”则停止复制,最后使用printf函数输出字符串str2。
5.字符串移动函数memmove
字符串移动函数memmove的函数原型如下。
voidmemmove(voiddest,void*src,int len);
其中,参数dest为目标字符串,src为源字符串,len为复制长度。字符串移动函数memmove功能与memcpy相同,同样用于复制字符串,但是复制区间src与dest可以发生交迭。该函数执行的操作是从src所指向的字符串中复制len个字符到dest字符串中,其返回值指向dest中的最后一个字符的指针。程序示例如下。
include<string.h>//头文件
include<stdio.h>
include<reg51.h>
void main(void)//主函数
{
static char str[]=“this is line 1”//初始化字符串
“this is line2”
“this is line 3”;
//初始化串口
printf(“str before=%s\n”,str);//输出移动前的字符串
memmove(&str[0],&str[15],32);//调用函数
printf(“str after=%s\n”,str);//输出移动后的字符串
}
该程序可以在KeilµVision3编译环境中执行,运行的结果如下。
str before=this is line1 this is line2 this is line3
str after=this is line2 this is line3
在该程序中,首先初始化了字符串str,接着调用memmove函数移动字符串,最后使用printf函数将运行的结果通过串口输出。
6.字符串填充函数memset
字符串填充函数memset的函数原型如下。
voidmemset(voids,char val,int len);
其中,参数s为待填充的字符串,val为填充字符,len为填充的长度。字符串填充函数memset用于按规定的字符填充字符串。该函数执行的操作是用字符val来填充字符串s,共填充len个单元。程序示例如下。
include<string.h>//头文件
include<stdio.h>
include<reg51.h>
void main(void)//主函数
{
static char str[]=“this is line 1”;
//初始化串口
printf(“str before=%s\n”,str);
memset(str,‘a’,sizeof(str));//调用函数
printf(“str after=%s\n”,str);//输出填充后的结果
}
该程序可以在KeilµVision3编译环境中执行,运行的结果如下。
str before=this is line 1
str after=aaaaaaaaaaaaaaa
在该程序中,首先初始化了字符串str,接着调用memset函数将字符串全部填充为字符“a”,最后使用printf函数输出填充后的结果。
7.字符串追加函数strcat
字符串追加函数strcat的函数原型如下。
voidstrcat(chars1,char*s2);
其中,参数s1为目标字符串,s2为待复制的字符串。字符串追加函数strcat用于复制字符串到另一个字符串的尾部。该函数执行的操作是将字符串s2复制到字符串s1的尾部。其中字符串s1要有足够的大小来保存两个字符串。该函数的返回值是指向字符串s1中首字符的指针。程序示例如下。
include<string.h>//头文件
include<stdio.h>
include<reg51.h>
void main(void)//主函数
{
static char str1[50]=“this is str1”;
static char str2[]=“this is str2”;
//初始化串口
strcat(str1,str2);//字符串追加
printf(“str1=%s\n”,str1);
}
该程序可以在KeilµVision3编译环境中执行,运行的结果如下。str1=this is str1 this is str2
在该程序中,首先初始化了字符串str1和str2,接着调用strcat函数将字符串str2复制到字符串str1的尾部,最后输出总的字符串。
8.指定长度的字符串追加函数strncat
指定长度的字符串追加函数strncat的函数原型如下。
voidstrncat(,chars1,char*s2,int n);
其中,参数s1为目标字符串,s2为待复制的字符串,n为复制的长度。指定长度的字符串追加函数strncat用于复制指定长度的字符串到另一个字符串的尾部。该函数执行的操作是从字符串s2中复制n个字符添加到字符串s1的尾部。其中,如果字符串s2的长度比n小,则将全部复制字符串s2(包括字符串结束符)。程序示例如下。
include<string.h>//头文件
include<stdio.h>
include<reg51.h>
void main(void)//主函数
{
static char str1[50]=“this is str1”;
static char str2[]=“this is str2”;
//初始化串口
strncat(str1,str2,9);//追加字符串
printf(“str1=%s\n”,str1);
}
该程序可以在KeilµVision3编译环境中执行,运行的结果如下。
str1=this is str1 this is s
在该程序中,首先初始化了字符串str1和str2,接着调用strncat函数从字符串str2中复制9个字符到字符串str1的尾部,最后使用printf函数输出字符串。
9.字符串比较函数strcmp
字符串比较函数strcmp的函数原型如下。
char strcmp(chars1,chars2);
其中,参数s1和s2为待比较的字符串。字符串比较函数strcmp用于比较两个字符串的大小。该函数执行的操作是比较字符串s1和s2,如果两者相等则返回0;如果s1>s2,则返回一个正数;如果s1<s2,则返回一个负数。程序示例如下。
include<string.h>//头文件
include<stdio.h>
include<reg51.h>
void main(void)//主函数
{
static char str1[]=“012345abcd”;
static char str2[]=“012345abcde”;
char i;
//初始化串口
i=strcmp(str1,str2);//调用函数
if(i>0)//输出比较结果
printf(“str1>str2\n”);
else if(i<0)
printf(“str1<str2\n”);
else
printf(“str1==str2\n”);
}
该程序可以在KeilµVision3编译环境中执行,运行的结果如下。
str1<str2
在该程序中,首先初始化了字符串str1和str2,接着调用strcmp函数比较字符串str1和str2,最后输出字符串比较的结果。
10.包含结束符的字符串比较函数strncmp
包含结束符的字符串比较函数strncmp的函数原型如下。
charstrncmp(chars1,char*s2,int n);
其中,参数s1和s2为待比较的字符串,n为比较的长度。包含结束符的字符串比较函数strncmp用于比较两个字符串的大小。该函数执行的操作是比较字符串s1和s2的前n个字符,如果两者相等则返回0;如果s1>s2,则返回一个正数;如果s1<s2,则返回一个负数。其和memcmp函数不同之处在于,如果字符串的长度小于n,则strncmp函数比较到字符串结束符后便停止。程序示例如下。
include<string.h>//头文件
include<stdio.h>
include<reg51.h>
void main(void)//主函数
{
static char str1[]=“012345abcd”;
static char str2[]=“012345abcde”;
char i;
//初始化串口
i=strncmp(str1,str2,20);//调用函数
if(i>0)//输出比较结果
printf(“str1>str2\n”);
else if(i<0)
printf(“str1<str2\n”);
else
printf(“str1==str2\n”);
}
该程序可以在KeilµVision3编译环境中执行,运行的结果如下。
str1<str2
在该程序中,首先初始化了字符串str1和str2,接着调用strncmp函数比较字符串str1和str2。由于strncmp函数中设置为比较20个字符,而字符串长度不够20个。因此,该函数比较到字符串结束符便停止比较。
11.字符串覆盖函数strcpy
字符串覆盖函数strcpy的函数原型如下。
charstrcpy(chars1,char*s2);
其中,参数s1为目标字符串,s2为源字符串。字符串覆盖函数strcpy用于将一个字符串覆盖另一个字符串。该函数执行的操作是将字符串s2(包括结束符)复制到字符串s1中的第1个字符指针处。该函数和strcat函数的不同之处在于,strcat函数将字符串s2复制到字符串s1的末尾。程序示例如下。
include<string.h>//头文件
include<stdio.h>
include<reg51.h>
void main(void)//主函数
{
static char str1[50]=“this is str1”;
static char str2[]=“this is str2”;
//初始化串口
strcpy(str1,str2);//调用函数
printf(“str1=%s\n”,str1);
}
该程序可以在KeilµVision3编译环境中执行,运行的结果如下。
str1=this is str2
在该程序中,首先初始化了字符串str1和str2,接着调用strcpy函数将字符串str2复制到str1的前面,并覆盖原字符串,最后输出新的字符串str1。
12.指定长度的字符串覆盖函数strncpy
指定长度的字符串覆盖函数strncpy的函数原型如下。
charstrncpy(chars1,char*s2,int n);
其中,参数s1为目标字符串,s2为源字符串,n为长度。指定长度的字符串覆盖函数strncpy用于将一个指定长度的字符串覆盖另一个字符串。该函数执行的操作是从字符串s2(包括结束符)中复制n个字符到字符串s1中的第1个字符指针处。如果字符串s2的长度小于n,则s1串以0补齐到长度n。程序示例如下。
include<string.h>//头文件
include<stdio.h>
include<reg51.h>
void main(void)//主函数
{
static char str1[50]=“this is str1”;
static char str2[]=“that is str2”;
//初始化串口
strncpy(str1,str2,4);//调用函数
printf(“str1=%s\n”,str1);
}
该程序可以在KeilµVision3编译环境中执行,运行的结果如下。
str1=that is str1
在该程序中,首先初始化了字符串str1和str2,接着调用strncpy函数从字符串str2中复制4个字符到str1的前面,并覆盖原字符串,最后使用printf函数输出新的字符串。
13.获取字符个数函数strlen
获取字符个数函数strlen的函数原型如下。
charstrlen(chars1);
其中,参数s1为输入字符串。strlen函数的功能是获取字符串s1中的字符个数,返回值的大小不包括最后的字符串结束符。程序示例如下。
include<string.h>//头文件
include<stdio.h>
include<reg51.h>
void main(void)//主函数
{
static char str[50]=“this is line 1”;
int i;
//初始化串口
i=strlen(str);//获取字符个数
printf(“strlen(str)=%d\n”,i);//输出结果
}
该程序可以在KeilµVision3编译环境中执行,运行的结果如下。
strlen(str)=15
在该程序中,首先初始化了字符串str,接着调用strlen函数获取该字符串中字符的个数,最后使用printf函数输出结果。
14.搜索字符串函数strstr
搜索字符串函数strstr的函数原型如下。
charstrstr(const chars1,char*s2);
其中,参数s1为目标字符串,s2为搜索的字符串。搜索字符串函数strstr用于搜索字符串出现的位置。该函数执行的操作是在字符串s1中搜索第一次出现字符串s2的位置,并返回该处的指针。如果字符串s1中不包括字符串s2,则返回一个空指针。
提示strstr函数返回的是一个字符位置指针,将该指针减去源字符串首地址指针,可得到子串的偏移位置。
程序示例如下。
include<string.h>//头文件
include<stdio.h>
include<reg51.h>
void main(void)//主函数
{
static char str[50]=“this is str1”;
char*p;
//初始化串口
p=strstr(str,“str”);//调用函数
printf(“p=strstr(str,\”is\“)=%s\n”,p);//输出结果
}
该程序可以在KeilµVision3编译环境中执行,运行的结果如下。
p=strstr(str,“str”)=str1
在该程序中,首先初始化了字符串str,接着调用strstr函数返回字符串“str”处的指针,最后使用printf函数输出结果。
15.搜索字符函数strchr
搜索字符函数strchr的函数原型如下。
charstrchr(chars1,char c);
其中,参数s1为目标字符串,c为待搜索的字符。搜索字符函数strchr用于搜索字符出现的位置。该函数执行的操作是搜索字符串s1中是否包含字符c,如果包含则返回第一次指向该字符的指针,否则返回空指针NULL。被搜索的字符可以是串结束符,此时返回值便是指向串结束符的指针。程序示例如下。
include<string.h>//头文件
include<stdio.h>
include<reg51.h>
void main(void)//主函数
{
static char str[50]=“this is str1”;
char*p;
//初始化串口
p=strchr(str,‘r’);//搜索字符“r”
if(p!=NULL)
printf(“found a‘r’at%s\n”,p);//输出结果
}
该程序可以在KeilµVision3编译环境中执行,运行的结果如下。
found a‘r’at r1
在该程序中,首先初始化了字符串str,接着调用strchr函数查找字符“l”,并返回指向该字符的指针,最后使用printf函数输出结果。
16.返回位置值的字符搜索函数strpos
返回位置值的字符搜索函数strpos的函数原型如下。
int strpos(char*s1,char c);
其中,参数s1为目标字符串,c为搜索的字符。返回位置值的字符搜索函数strpos用于搜索并返回字符出现的位置。strpos函数的功能与strchr类似,只不过返回值不同。该函数执行的操作是查找并返回字符c在字符串s1中第一次出现的位置值,没有找到该字符则返回-1,s1串首字符的位置值是0。程序示例如下。
include<string.h>//头文件
include<stdio.h>
include<reg51.h>
void main(void)//主函数
{
static char str[40]=“this is str1”;
int i;
//初始化串口
i=strpos(str,‘s’);//调用函数
if(i!=-1)
printf(“found a‘s’at%d\n”,i);//输出结果
}
该程序可以在KeilµVision3编译环境中执行,运行的结果如下。
found a‘s’at 3
在该程序中,首先初始化了字符串str,接着调用strpos函数查找字符“s”,并返回位置值,最后使用printf函数输出结果。
17.字符包含函数strrchr
字符包含函数strrchr的函数原型如下。
charstrrchr(chars1,char c);
其中,参数s1为目标字符串,c为查找的字符。字符包含函数strrchr用于检查字符串中是否包含某字符。该函数执行的操作是搜索字符串s1中是否包含字符c,如果包含将返回最后一次指向该字符的指针,否则将返回NULL。被搜索的字符也可以是字符串结束符,此时返回值将是指向字符串结束符的指针。程序示例如下。
include<string.h>//头文件
include<stdio.h>
include<reg51.h>
void main(void)//主函数
{
static char str[40]=“this is str1”;
char*p;
//初始化串口
p=strrchr(str,‘s’);//调用函数
if(p!=NULL)
printf(“found the last‘s’at%s\n”,p);//输出结果
}
该程序可以在KeilµVision3编译环境中执行,运行的结果如下。
found the last‘s’at str1
在该程序中,首先初始化了字符串str,接着调用strrchr函数查找字符“s”,并返回最后一次指向该字符的指针,最后使用printf函数输出结果。
18.返回位置值的字符包含函数strrpos
返回位置值的字符包含函数strrpos的函数原型如下。
int strrpos(char*s1,char c);
其中,s1为目标字符串,c为查找的字符。strrpos函数的功能与strrchr类似,只不过返回值不同,返回位置值的字符包含函数strrpos用于检查字符串中是否包含某字符。该函数执行的操作是查找并返回字符c在字符串s1中最后一次出现的位置值,s1串首字符的位置值是0,如果没有找到该字符则返回-1。程序示例如下。
include<string.h>//头文件
include<stdio.h>
include<reg51.h>
void main(void)//主函数
{
static char str[40]=“this is str1”;
int i;
//初始化串口
i=strrpos(str,‘s’);//调用函数
if(i!=-1)
printf(“found the last‘s’at%d\n”,i);//输出结果
}
该程序可以在KeilµVision3编译环境中执行,运行的结果如下。
found the last‘s’at 8
在该程序中,首先初始化了字符串str,接着调用strrpos函数查找字符“s”,并返回最后一次出现的位置值,最后使用printf函数输出结果。
19.在指定字符集中查找不包含字符函数strspn
在指定字符集中查找不包含字符函数strspn的函数原型如下。
int strspn(chars1,charset);
其中,参数s1为目标字符串,set为字符集。在指定字符集中查找不包含字符函数strspn用于查找不包含在指定字符集中的字符。该函数执行的操作是搜索字符串s1中第一个不包含在set串中的字符,返回值是字符串s1中包括在set中的字符的个数。如果字符串s1中所有的字符都包含在set中,则返回s1中字符的个数(不包括结束符)。如果字符集set是空字符串则返回0。程序示例如下。
include<string.h>//头文件
include<stdio.h>
include<reg51.h>
void main(void)//主函数
{
static char str[50]=“this is line 12”;
char set[]=“this line 12345”;
int i;
//初始化串口
i=strspn(str,set);//调用函数
printf(“strspn(str,set)=%d\n”,i);//输出结果
}
该程序可以在KeilµVision3编译环境中执行,运行的结果如下。
strspn(str,set)=15
在该程序中,首先初始化了字符串str以及字符集set,接着调用strspn函数,最后使用printf函数输出结果。
20.在指定字符集中查找包含字符函数strcspn
在指定字符集中查找包含字符函数strcspn的函数原型如下。
int strcspn(chars1,charset);
其中,参数s1为目标字符串,set为字符集。在指定字符集中查找包含字符函数strcspn用于查找包含在指定字符集中的字符。该函数执行的操作是搜索的是第一个包含在set串中字符,返回值是字符串s1中包括在set中的字符的个数。如果s1中所有的字符都包含在set中,则返回字符串s1的长度(不包括末尾的字符串结束符)。如果字符集set是空字符串,则返回0。程序示例如下。
include<string.h>//头文件
include<stdio.h>
include<reg51.h>
void main(void)//主函数
{
static char str[40]=“this is line 12”;
char set[]=“123456”;
int i;
//初始化串口
i=strcspn(str,set);//调用函数
printf(“strcspn(str,set)=%d\n”,i);
}
该程序可以在KeilµVision3编译环境中执行,运行的结果如下。
strcspn(str,set)=13
在该程序中,首先初始化了字符串str以及字符集set,接着调用strcspn函数,最后使用printf函数输出结果。
21.查找第一个包含字符函数strpbrk
查找第一个包含字符函数strpbrk的函数原型如下。
charstrpbrk(chars1,char*set);
其中,参数s1为目标字符串,set为字符集。查找第一个包含字符函数strpbrk用于查找第一个包含在指定字符集中的字符。该函数执行的操作是搜索字符串s1中第一个包含在set串中的字符,返回一个指向搜索到的字符的指针,如果未找到将返回NULL。程序示例如下。
include<string.h>//头文件
include<stdio.h>
include<reg51.h>
void main(void)//主函数
{
static char str[40]=“this is line 12”;
char set[]=“23456”;
char*p;
//初始化串口
p=strpbrk(str,set);//调用函数
printf(“p=strpbrk(str,set)=%s\n”,p);
}
该程序可以在KeilµVision3编译环境中执行,运行的结果如下。
p=strpbrk(str,set)=2
在该程序中,首先初始化了字符串str以及字符集set,接着调用strpbrk函数,最后使用printf函数输出结果。
22.查找最后一个包含字符函数strrpbrk
其函数原型如下。
charstrrpbrk(chars1,char*set);
其中,s1为目标字符串,set为字符集。查找最后一个包含字符函数strrpbrk用于查找最后一个包含在指定字符集中的字符。该函数执行的操作是搜索字符串s1中最后一个包含在字符集set中的字符,返回值指向搜索到的字符的指针,如果未找到将返回NULL。程序示例如下。
include<string.h>//头文件
include<stdio.h>
include<reg51.h>
void main(void)//主函数
{
static char str[40]=“this is line 123”;//初始化字符串
char set[]=“123456”;
char*p;
//初始化串口
p=strrpbrk(str,set);//调用函数
printf(“p=strrpbrk(str,set)=%s\n”,p);
//输出结果
}
该程序可以在KeilµVision3编译环境中执行,运行的结果如下。
p=strrpbrk(str,set)=23
在该程序中,首先初始化了字符串str以及字符集set,接着调用strrpbrk函数,最后使用printf函数输出结果。