4.5题目: 输入一个字符串,同时输入帧头和帧尾(可以是多个字符),将该字符串中合法的帧识别出来.

来源:互联网 发布:上海犇迩网络骗局 编辑:程序博客网 时间:2024/04/29 04:47
/*题目: 输入一个字符串,同时输入帧头和帧尾(可以是多个字符),将该字符串中合法的帧识别出来.提示:帧头和帧尾分别是head和tail  字串”asdheadhauboisoktail”中headhauboisoktail是合法帧*/
#include <stdio.h>
#include <string.h>
int fun(char *ptr,char *head,char *tail)
{
char *temp;
while(*ptr != '\0')
{
if(strncmp(ptr,head,strlen(head)) == 0)
{
temp = ptr;
ptr+=strlen(head);
while(*ptr != '\0')
{
if(strncmp(ptr,tail,strlen(tail)) == 0)
{
*(ptr+strlen(tail)) = '\0';
  printf("%s\n",temp);
return 0;
       }
ptr++;
}
printf("find head not find tail!\n");
break;
}
else
ptr++;
}
return 0;
}
int main()
{
char src[100];
char head[20];
char tail[20];
printf("please input string:\n");
scanf("%s",src);
printf("please input head:\n");
scanf("%s",head);
printf("please input tail:\n");
scanf("%s",tail);


printf("the result:\n");
fun(src,head,tail);
return 0;
}
0 0
原创粉丝点击