我正在寻求有关创建函数的帮助。
函数 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/