c - 循环文本以匹配 C 中两个链接列表之间的字符计数

标签 c

我想循环此文本以匹配链接列表中元素的计数。

char myBaseArray[] = "close your eyes ";

我有一个由 int (0 和 1)组成的链表,如下所示:

linkedListOne = 1 -> 0 -> 0 -> 0 -> 1 -> 1 -> 1 -> 1 -> 1 -> 1 -> 1 -> 0 -> 0 -> 0  

我想创建第二个具有相同数量的 0 和 1 的链表,并循环遍历我的基本数组以获取文本。换句话说,1 和 0 是字母,但我想在基本数组中保留空格。

示例 1:

linkedListOne = 1 -> 0   (2 elements in my list)
linkedListTwo = c -> l   (2 letters)

示例 2:

linkedListOne = 1 -> 0 -> 1 -> 0 -> 1 -> 0 -> 1 -> 0 -> 1 -> 0 -> 1 -> 0 -> 1 -> 0 -> 1 -> 0 -> 1 -> 0 -> 1 -> 0 (20 elements in my list)
linkedListTwo = c -> l -> o -> s -> e ->(blank)-> y -> o -> u -> r ->(blank)-> e -> y -> e -> s ->(blank)->c -> l -> o -> s -> e->(blank)->y -> o(20 letters)

如您所见,空白不算作 0 或 1。

示例 2 更复杂,因为我必须在链接列表中添加空格,因此当我打印它时,它将输出:

close your eyes close yo

另一个具有很长列表的输出可能是:

close your eyes close your eyes close your eyes close your eyes close your eyes 

我的链表工作得很好,并且我已经实现了一个计数函数,所以我知道 1 和 0 的数量。我不确定如何循环遍历我的基本数组以匹配该计数。

最佳答案

我结束了这样做:

    int i=0;
    int j=0;
    while(i != LETTER_COUNT)
    {
        char c = myBaseArray[j % myBaseArray_LENGTH];
        if(c == ' '){
            linkedListTwo = add(linkedListTwo,  c);
            j++;
        }
        else{
            linkedListTwo= add(linkedListTwo,  c);
            j++;
            i++;    
    }
}

关于c - 循环文本以匹配 C 中两个链接列表之间的字符计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28204427/

相关文章:

c - C语言如何从文件中读取数据?

c - 如何在内核中记录大量数据

C 程序输出错误

c - 如何使用 simplescalar 运行 C 代码?

python - 使用 Python/C API 获取解释器中 PyString 的值作为 C 程序中的 CString

c - 为什么我的 C 代码会崩溃?

c - C 中函数将指向局部静态/全局变量的指针作为左值返回

c - 浮点除法返回 inf 和有限值是什么情况?

c - 如何在 C 中调试此错误 : function definition is not allowed here?

c - 防止 __start 入口点被优化掉