在C中的数组中从右向左滚动一个单词?

在C中的数组中从右向左滚动一个单词?,c,arrays,C,Arrays,假设您有(可以是任意长度的字符串) 还有一些空的“缓冲区”数组,限制为10个字符 char buffer[12] = " "; //initialized with 10 spaces, leaving room for null character. 例如,在第一次传递之后,缓冲区将是“C”;(C前面的9个空格) 接下来是“Co”;(前面8格) 下面的代码在一定程度上可以工作,但当它达到较大的i值时,它开始添加多个字符,然后反复添加第一个字符 int i = 0; in

假设您有(可以是任意长度的字符串)

还有一些空的“缓冲区”数组,限制为10个字符

char buffer[12] = "            "; //initialized with 10 spaces, leaving room for null character.
例如,在第一次传递之后,
缓冲区
将是“C”;(C前面的9个空格)

接下来是“Co”;(前面8格)

下面的代码在一定程度上可以工作,但当它达到较大的i值时,它开始添加多个字符,然后反复添加第一个字符

int i = 0;
int j;
int k = 0;
char word[] = "Coding is really fun";
char buffer[12] = "            ";
buffer[11]='\0';
buffer[10] = word[0];

while(1){
k = 0;
for (j = 10 - i; j < 11; j++){
    buffer[j] = word[k];
    k++;
}
i++;
}

假设您有一个循环源文本,它由
单词
组成,后跟与缓冲区一样多的空格。这个虚构的圆形源文本的大小为

size_t wordsize = strlen(word);
size_t buffsize = strlen(buffer);
size_t icstsize = wordsize + buffsize;
每次移动缓冲区时,都会更改其中的每个字符。文本的第一部分向左移动,最后一个字符取自虚构的圆形源文本。让我们跟踪虚拟圆形源文本中的位置:

size_t position = 0; // start at character zero of word
现在

while(1)
{
//移动缓冲区
用于(尺寸i=0u;i<(尺寸-1u);i++)
缓冲区[i]=缓冲区[i+1u];
//填写最后一个字符
缓冲区[buffsize-1u]=位置
道格·柯里的代码真的非常棒,刚刚经过测试。
编译时使用:
gcc-Wall shift\u buffer.c-std=c11

下面是他的代码,可以进行测试(只是为了简化测试):
再说一遍,这是他的代码

#define _BSD_SOURCE
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>

int main(void)
{
        char word[] = "Coding is really fun";
        char buffer[12] = "            "; //initialized with 10 spaces, leaving room for null character.

        size_t wordsize = strlen(word);
        size_t buffsize = strlen(buffer);
        size_t icstsize = wordsize + buffsize;

        size_t position = 0; // start at character zero of word

        while (1)
        {
                // shift the buffer
                for (size_t i = 0u; i < (buffsize - 1u); i++)
                        buffer[i] = buffer[i + 1u];

                // fill in the last character
                buffer[buffsize - 1u] = position < wordsize ? word[position] : ' ';

                // increment the position in the imaginary circular source text
                position = (position + 1u) % icstsize;
                fprintf(stderr, "[%s] \r", buffer);
                usleep(500000);
        }

}
\define\u BSD\u源
#包括
#包括
#包括
#包括
内部主(空)
{
char word[]=“编码真的很有趣”;
字符缓冲区[12]=“”;//初始化为10个空格,为空字符留出空间。
字号=strlen(字);
大小\u t buffsize=strlen(缓冲区);
size\u t icstsize=wordsize+buffsize;
size\u t position=0;//从单词的字符0开始
而(1)
{
//移动缓冲区
用于(尺寸i=0u;i<(尺寸-1u);i++)
缓冲区[i]=缓冲区[i+1u];
//填写最后一个字符
缓冲区[buffsize-1u]=位置
这不是
c
语法。你真正使用的语言是什么?
char[12]buffer;buffer=”“
不是有效的C,编译器应该给您一条错误消息。有什么想法吗?-不是有效的问题。请发布您的工作代码,并用正确的语言标记此问题。@MarkBenningfield代码现在已发布,语言为C,这就是我标记的语言。
size_t position = 0; // start at character zero of word
while (1)
{
    // shift the buffer
    for (size_t i = 0u; i < (buffsize - 1u); i++)
        buffer[i] = buffer[i + 1u];
    // fill in the last character
    buffer[buffsize - 1u] = position < wordsize ? word[position] : ' ';
    // increment the position in the imaginary circular source text 
    position = (position + 1u) % icstsize;
}
#define _BSD_SOURCE
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>

int main(void)
{
        char word[] = "Coding is really fun";
        char buffer[12] = "            "; //initialized with 10 spaces, leaving room for null character.

        size_t wordsize = strlen(word);
        size_t buffsize = strlen(buffer);
        size_t icstsize = wordsize + buffsize;

        size_t position = 0; // start at character zero of word

        while (1)
        {
                // shift the buffer
                for (size_t i = 0u; i < (buffsize - 1u); i++)
                        buffer[i] = buffer[i + 1u];

                // fill in the last character
                buffer[buffsize - 1u] = position < wordsize ? word[position] : ' ';

                // increment the position in the imaginary circular source text
                position = (position + 1u) % icstsize;
                fprintf(stderr, "[%s] \r", buffer);
                usleep(500000);
        }

}