#include <stdio.h>
int head_flag = 0;
int success = 0;
int my_strlen(char *str)
{
int i = 0;
while(*str != '\0')
{
i++;
str++;
}
return i;
}
char *head_add(char *str,char *head)
{
if(str == NULL ||head == NULL )
{
return NULL;
}
if(my_strlen(str) < my_strlen(head) )
{
return NULL;
}
char *temp ;
int head_len = my_strlen(head);
int head_count = 0;
while( *str != '\0' && !head_flag )
{
while(*str == *head)
{
str++;
head++;
head_count++;
if(head_count == head_len)
{
head_flag = 1;
temp = str - head_len ;
break;
}
}
str++;
}
if (head_flag)
{
//printf("%d\n",temp);
return temp;
}
else
{
printf("string illgeal");
}
}
char *tail_add(char *str,char *tail)
{
if(str == NULL ||tail == NULL )
{
return NULL;
}
if( my_strlen(str) < my_strlen(tail) )
{
return NULL;
}
int tail_len = my_strlen(tail);
int tail_count = 0;
if(head_flag)
{
while( *str != '\0' && !success)
{
while(*str == *tail)
{
str++;
tail++;
tail_count++;
if(tail_count == tail_len)
{
success = 1;
break;
}
}
str++;
}
}
if(success)
{
//printf("%d\n",str-1);
return str - 1 ;
}
else
{
printf("string illgeal");
return NULL;
}
}
int main()
{
char *str = "adsheadasitailwer";
char *head = "head";
char *tail = "tail";
char *str_start = head_add(str,head);
char *str_end = tail_add(str,tail);
printf("before:\n");
printf("%s\n",str);
printf("after:\n");
int n = str_end -str_start;
int i = 0;
// printf("%d",n);
for(i = 0; i <n ;i++)
{
printf("%c",*str_start);
str_start ++;
}
printf("\n");
return 0;
}
自己写的一个关于 帧头帧尾的
题目: 输入一个字符串,同时输入帧头和帧尾(可以是多个字符),将该字符串中合法的帧识别出来.
提示:帧头和帧尾分别是head和tail 字符串”asdheadhauboisoktail”中headhauboisoktail是合法帧
这个题目感觉要完善的有很多 比如有多个head和多个tail,那么到底是以那个head为帧头,那个tail为帧尾。。。。
运行如下