#include<stdio.h>
#define N 100
int main()
{
char str[N] = {0};
char head[N] = {0}; //定义数组并初始化
char tail[N] = {0};
char *a = NULL;
char *b = NULL; //定义指针并初始化
char *c = NULL;
int i = 0;
int j = 0; //定义整型变量
int k = 0;
a = str;
b = head; //将数组的首地址给指针
c = tail;
printf("please input str\n");
scanf("%s",str);
printf("please input head\n");
scanf("%s",head);
printf("please input tail\n");
scanf("%s",tail);
for(i = 0; i < strlen(str); i++)
{
if(*(a + i) == *(b + j))
{
j++;
}
else
{
j = 0; //通过循环从字符串中找到帧头;
}
if(j == strlen(head))
{
j = i - j + 1; //找完帧头后将帧头在str中的首个字符的位置给j
break;
}
}
if(i == strlen(str))
{
printf("no head\n"); //找不到帧头则返回
return 0;
}
k = strlen(tail) - 1;
for(i = strlen(str) - 1; i >= 0; i--)
{
if(*(a + i) == *(c + k))
{
k--; //从str的尾部向前寻找针尾
}
else
{
k = strlen(tail) - 1;
}
if(k == -1)
{
k = i + strlen(tail) - 1; //找完后将针尾在str中尾部的位置给k
break;
}
}
if(i == -1)
{
printf("no tail\n"); //找不到帧尾则返回
return 0;
}
printf("the result is:\n");
for(j; j <= k; j++)
{
printf("%c",*(a + j)); //循环输出;
}
printf("\n");
a = NULL;
b = NULL; //防止野指针
c = NULL;
return 0;
}
#define N 100
int main()
{
char str[N] = {0};
char head[N] = {0}; //定义数组并初始化
char tail[N] = {0};
char *a = NULL;
char *b = NULL; //定义指针并初始化
char *c = NULL;
int i = 0;
int j = 0; //定义整型变量
int k = 0;
a = str;
b = head; //将数组的首地址给指针
c = tail;
printf("please input str\n");
scanf("%s",str);
printf("please input head\n");
scanf("%s",head);
printf("please input tail\n");
scanf("%s",tail);
for(i = 0; i < strlen(str); i++)
{
if(*(a + i) == *(b + j))
{
j++;
}
else
{
j = 0; //通过循环从字符串中找到帧头;
}
if(j == strlen(head))
{
j = i - j + 1; //找完帧头后将帧头在str中的首个字符的位置给j
break;
}
}
if(i == strlen(str))
{
printf("no head\n"); //找不到帧头则返回
return 0;
}
k = strlen(tail) - 1;
for(i = strlen(str) - 1; i >= 0; i--)
{
if(*(a + i) == *(c + k))
{
k--; //从str的尾部向前寻找针尾
}
else
{
k = strlen(tail) - 1;
}
if(k == -1)
{
k = i + strlen(tail) - 1; //找完后将针尾在str中尾部的位置给k
break;
}
}
if(i == -1)
{
printf("no tail\n"); //找不到帧尾则返回
return 0;
}
printf("the result is:\n");
for(j; j <= k; j++)
{
printf("%c",*(a + j)); //循环输出;
}
printf("\n");
a = NULL;
b = NULL; //防止野指针
c = NULL;
return 0;
}