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

xiaoxiao2021-02-28  99

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

提示:帧头和帧尾分别是headtail  字符串”asdheadhauboisoktail”headhauboisoktail是合法帧

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); } } int main() { char str[] = "asdheadhauboisoktailgdg"; findFrame(str, "head", "tail"); return 0; } 主函数里直接调用了一个子函数findFrame,这个子函数实现的功能就是去打印找打的合法帧。然后这个子函数又调用了一个子函数findStr,这个子函数的主要功能就是去找合法帧。将找到的帧头以后,返回它的地址,然后帧尾在从帧头后面继续去遍历寻找帧尾。找到帧尾以后再返回帧尾的地址,然后将帧尾后面的那个字符赋为’\0’,直接从帧头的位置开始打印,直到遇到刚刚赋的’\0’。
转载请注明原文地址: https://www.6miu.com/read-61012.html

最新回复(0)