去除头帧和尾帧的字符串

昨天无聊的时候写了一个带头帧和尾帧的字符串,然后输出时,去除头帧和尾帧,我想这不是分分钟的事哦,但是粗心的我忘记了限定的条件,所以写出来铭记自己,时刻须谨慎!

一:头帧和尾帧的长度限定,即规定的头尾帧之前和后面都没有字符

实例代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_SIZE 1024

void getstr(char str[], int len_head, int len_tail)
{
    int i;
    int j = 0;
    char temp[MAX_SIZE];
    for(i = 0; i < strlen(str); i++)
    {
        if(i >= len_head)
        {
            if(i < (strlen(str) - len_tail))
	    {
		temp[j] = str[i];
		j++;
	    }
        }
    }
    temp[j] = '\0';
    
    int k = 0;
    printf("the del str is :\n");
    for(k = 0; k < j ; k++)
    {
	printf("%c",temp[k]);
    }
}

int main()
{
    char str[MAX_SIZE];
    char head[MAX_SIZE] = "head";
    char tail[MAX_SIZE] = "tail";
    printf("Please input str:\n");
    
    /*使用gets时,定义的数组长度要大,避免造成数组越界*/
    gets(str);
    getstr(str,strlen(head),strlen(tail));

    return 0;
}

结果:指定的头尾帧长度大小不能变

当头尾帧长度变时:输出的错误结果如下:


二:头帧和尾帧的长度可变,即在头帧和尾帧的标志位前后有数据不影响结果

实例代码:

/*****************************************************
copyright (C), 2016-2017, Lighting Studio. Co.,     Ltd. 
File name:ChenYangYang    Version:0.1    Date:2016/11/13 
Description:去除一个字符串的头尾帧,长度不限
Funcion List:
*****************************************************/

#include <stdio.h>
#include <string.h>

#define MAX_SIZE 1024

void find(char str[],char head[],char tail[])
{
    int i = 0;                 /*str字符串的起始坐标*/
    int j = 0;
    int k = 0;                   /*tail的起始坐标*/
    int times = 0;               /*循环次数*/
    int len_head;                /*head的长度*/
    int len_tail;                 /*tail的长度*/
    int count_head = 0;          /*记录次数*/
    int count_tail = 0;          /*记录次数*/  
    /*接收输出的字符,一定要初始化,不然输出合法的字符时,之前会有无用的乱码*/
    char temp[MAX_SIZE] = {0};   
    len_head = strlen(head);
    len_tail = strlen(tail);

    while(str[i] != '\0')        /*遍历str*/
    {
        if(str[i] == tail[k])     /*当str与尾帧的起始字符相等时,进入循环*/
        {
            count_tail++;         /*记录与尾帧相等的次数*/
            k++;                  
        }
        if(count_head == len_head)
        {
            /*当记录的次数与尾帧长度相等*/
            if(count_tail == len_tail)
            {
                printf("the str is :"); 

                /*输出合法字符串*/
                for(times = 0; times <= (j - len_tail); times++)
                {
                    printf("%c",temp[times]);
                }
                printf("\n");
                
            }
            temp[j] = str[i]; /*将str中除去头帧的字符赋给temp数组*/
            j++;
        }
        else if(str[i] == head[j])   /*str字符与头帧起始字符相等时*/
        {
            count_head++;
            j++;
        }
        else
        {
            j = 0;
            k = 0;
            count_head = 0;
            count_tail = 0;
        }
        i++;
    }
    printf("no str!\n");
    return ;  /*无返回值*/
}

int main()
{      
      /*输入一个带头帧和尾帧的字符串*/
	char str[MAX_SIZE];
       char head[] = "head";   /*头帧标志符号*/
       char tail[] = "tail";   /*尾帧标志符号*/

	printf("Please input str");
	scanf("%s",str);

    find(str,head,tail);

    return 0;
}

结果:




  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用串口通信库,如serial.h,来接收串口数据,并通过字符串匹配来筛选出固定字符串。 以下是一个示例程序: ```c #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> #include <string.h> #include <termios.h> #include <errno.h> #define BUF_SIZE 256 // 串口数据缓冲区大小 #define FRAME_HEAD "START" // 字符串 #define FRAME_TAIL "END" // 字符串 int main(void) { int fd; // 串口文件描述符 char buf[BUF_SIZE]; // 串口数据缓冲区 int len; // 串口数据长度 struct termios options; // 串口配置 // 打开串口设备 fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY); if (fd < 0) { perror("open"); exit(1); } // 配置串口 tcgetattr(fd, &options); cfsetispeed(&options, B9600); cfsetospeed(&options, B9600); options.c_cflag &= ~PARENB; options.c_cflag &= ~CSTOPB; options.c_cflag &= ~CSIZE; options.c_cflag |= CS8; options.c_cflag &= ~CRTSCTS; options.c_cflag |= CREAD | CLOCAL; options.c_iflag &= ~(IXON | IXOFF | IXANY); options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); options.c_oflag &= ~OPOST; options.c_cc[VMIN] = 1; options.c_cc[VTIME] = 0; tcsetattr(fd, TCSANOW, &options); // 读取串口数据并处理 while (1) { len = read(fd, buf, BUF_SIZE); if (len > 0) { buf[len] = '\0'; // 匹配 if (strstr(buf, FRAME_HEAD)) { // 匹配 if (strstr(buf, FRAME_TAIL)) { // 这里可以处理完整的数据 printf("Received frame: %s\n", buf); } } } } // 关闭串口设备 close(fd); return 0; } ``` 该程序通过串口接收数据,并匹配来筛选完整的数据。可以根据自己的实际需求修改字符串

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值