#include <stdio.h>
#include <string.h>
char *head_count(char *str, char *head)//找出帧头前面的字符串
{
char *temp;
int size_head = strlen(head);
int count1 = 0;
int head_flag = 0;
if(str == NULL || head == NULL)
{
return NULL;
}
if(strlen(str) < strlen(head))
{
return NULL;
}
while(*str != '\0' && !head_flag)//判断找出dead
{
while(*str == *head)
{
str++;
head++;
count1++;
if(count1 == size_head)
{
head_flag = 1;
temp = str - size_head;//temp表示帧头出现位置前的字符
break;
}
}
str++;
}
if(head_flag)
{
return temp;
}
else
{
printf("error!");
}
}
char *tail_count(char *str, char *tail)//找出到帧尾为止的所有的字符串
{
if(str == NULL || tail == NULL)
{
return NULL;
}
if(strlen(str) < strlen(tail))
{
return NULL;
}
int size_tail = strlen(tail);
int count2 = 0;
int tail_flag = 0;
int head_flag = 1;
if(head_flag)
{
while(*str != '\0' && !tail_flag)//判断寻找出帧尾
{
while(*str == *tail)
{
str++;
tail++;
count2++;
if(size_tail == count2)
{
tail_flag = 1;
break;
}
}
str++;
}
}
if(tail_flag)
{
return str - 1;//返回的是到帧尾结束的字符串
}
else
{
printf("error!");
}
}
int main()
{
int i;
char *str = "asdheadhauboisoktail";
char *head = "head";
char *tail = "tail";
char *str_head = head_count(str, head);
char *str_tail = tail_count(str,tail);
printf("输入的字符串为:");
printf("%s\n",str);
printf("合法的帧为:");
int n = str_tail - str_head;
for(i = 0;i < n;i++)//输出合法的帧
{
printf("%c",*str_head);
str_head++;
}
printf("\n");
return 0;
}