找合法帧

xiaoxiao2021-02-28  68

#include <stdio.h> #include <string.h> // 在字符串str中寻找字符串sub,如果找到返回从sub开始的字符串 char *findStr(char* str, char *sub) { char *p = NULL; int len = strlen(sub);  // 算字串的长度 while(*str) { if (strncmp(str, sub, len) == 0) { p = str; break; } str++; } return p; } // 找合法帧 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; }
转载请注明原文地址: https://www.6miu.com/read-66134.html

最新回复(0)