在C中的char数组中删除相同的连续行 [英] Remove identical, consecutive lines in a char array in C

查看:97
本文介绍了在C中的char数组中删除相同的连续行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个函数,该函数将检测char数组中是否存在相同的连续行.

I'm trying to create a function that will detect if there are consecutive lines in a char array that are identical.

例如,如果一个char数组包含:

For example, if a char array contained:

你好

你好

你好

你好

然后将数组更改为

你好

基本上,我想检测连续的相同行,并删除它们,以便仅保留其中一行.如果一行与先前的行相同,但它们不是连续的,那很好.

Essentially, I want to detect the consecutive, identical lines, and delete them so only one of the lines remains. If one line is identical to a earlier line, but they are not consecutive, then it's fine.

真的,整行不必相同,但是至少前79个或MAXCHARS必须相同.

Really, the whole line doesn't have to be identical, but at least the first 79, or MAXCHARS, have to be identical.

此外,我不想通过写入中间文件来执行此操作.理想情况下,我将数据存储在数组中.

Additionally, I don't want to do this by writing to an intermediate file. Ideally, I would store data in arrays instead.

我在想类似的东西

    int deleteRepeats(char *a)
 {
         int i;
          for (i=0; i<=MAXCHARS; i++) {
          if (a[i] != '\n')
              /* copy into new array /*
        }
 }

但是我有点迷路了.我现在不想打印数组,因为稍后我将在程序中再次对其进行更改.我仍然需要使用一个.

but I'm somewhat lost. I don't want to print the array right now, because I will be altering it again later in my program; I still need to use a.

任何帮助/解决方案都将不胜感激.谢谢.

Any help/solution is greatly appreciated. Thank you.

推荐答案

一些指导:

  • 您最多只能复制一次每个字符-向后复制到其目的地.不需要多余的副本.
  • 您将在数组中拥有一个最终完成到此处"位置,并拥有一个正在处理/读取"位置.
  • 跟踪可能重复的当前全线,并与每条新线匹配以检查其是否为重复.
  • 以整行而不是字符前进-查找下一行时除外.

这篇关于在C中的char数组中删除相同的连续行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆