c - 在 C 中,如何删除 char 数组中相同且连续的行?

标签 c arrays char

我正在寻求有关创建函数的帮助。

函数 deleteIdents() 将删除 char 数组中的相同行,前提是它们是连续的。它将保留相同的行之一。

我不需要检查整行是否相同。只需前 79 个字符 MAXCHARS 就可以满足这种情况。

因此,例如,如果我的数组包含

Hello World
Hi World
Hello World
Hello World
Hello World
Hi there

会变成

Hello World
Hi World
Hello World
Hi there

在我看来,该函数类似于:

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

但我不确定。如果您有解决方案,我会很高兴和感激:)

最佳答案

第一行比第二行好,比较它们是否相等进入循环直到它们不相等。所以这是代码:

char *first_line = malloc(MAXLINE);
char *second_line = malloc(MAXLINE);

getline(first_line);

do {
   getline(second_line);
} while (strcmp (first_line, second_line));

对于 getline() 实现搜索 SO 有很多例子。或者 here你有我的。

关于c - 在 C 中,如何删除 char 数组中相同且连续的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50034090/

相关文章:

c - '0'字符和<<运算符在本程序中的重要性

C 中的 Caesar Cypher 给出不正确的输出

c - child 的多路树内存分配

c - GNU mingw 编译器错误 : sh: gcc: command not found

python - 计算字符串中数组的出现次数

javascript - 如何从Javascript中的多维数组中找到距离当前日期最近的日期

c++ - 无法确定为什么函数调用中从 char* 到 char 的无效转换

c - 使用 strcat 向 char** 元素添加空格

c - C 双指针分配问题

c++ - 将 cin 用于 char 数组