如何解决合法帧

来源:互联网 发布:淘宝在哪开店 编辑:程序博客网 时间:2024/04/30 15:19
#include <stdio.h>
#include <string.h>


// 在字符串str中寻找字符串sub,如果找到返回从sub开始的字符串
char *findStr(char* str, char *sub)
{
char *p = NULL;
int len = strlen(sub);  // 算字串的长度
while(*str)
{
if (strncmp(str, sub, len) == 0)
{
p = str;
break;
}
str++;
}

return p;
}


// 找合法帧
void findFrame(char *str, char *head, char *tail)
{
char *phead = findStr(str, head);  // 找帧头的位置
char *ptail = findStr(str, tail);  // 找帧尾的位置

if (phead != NULL && ptail != NULL)
{
ptail += strlen(tail);
*ptail = '\0';

printf ("%s\n", phead);
}
str++;




}


int main()
{
char str[] = "asdheadhauboisoktailgdg";
findFrame(str, "head", "tail");

return 0;
}
原创粉丝点击