如何逐个打印每个字符?

时间:2021-04-10 15:07:53

I'm trying to print the characters in this array one by one in a table with 7 rows and 4 columns. However, the only output I get is the whole alphabet in every column and row. How do I print each character one by one in the order I want?
Note that the table is supposed to end with a space and a ..

我正在尝试在一个包含7行和4列的表中逐个打印此数组中的字符。但是,我得到的唯一输出是每列和每行的整个字母。如何按照我想要的顺序逐个打印每个字符?请注意,该表应该以空格和...结尾。

The output should look like this:

输出应如下所示:

a b c d
e f g h
h i j k
l m n o
p q r s
t u v w
y z   .

This is my code:

这是我的代码:

struct pagestruct sequence[ROWS];
char alfabet[] = "abcdefghijklmnopqrstuvwxyz .";

for (int i = 0; i < ROWS; i++) {
  printf("\n");
  for (int k = 0; k < COLUMNS; k++) {
    strcpy(sequence[k].page, alfabet);
    printf("%s", sequence[k].page);
  }
}

EDIT:

This is the header file:

这是头文件:

#define COLUMNS 4
#define ROWS 7 

struct pagestruct
{
  char page[COLUMNS];
};

1 个解决方案

#1


1  

Here you have the function. If the string is terminated by the " ." it takes the data up to '.'. If not it takes the data up to number of rows and columns or the end of the string to avoid an UB. If the string is too short the rest of the table is filled with spaces. Of course you can break at this point but i do not know if it is the idea.

在这里你有这个功能。如果字符串被“。”终止。它将数据带到'。'。如果不是,它将数据增加到行数和列数或字符串的结尾以避免UB。如果字符串太短,表的其余部分将填充空格。当然你可以在这一点上打破,但我不知道这是不是这个想法。

Because you wanted to fill another table I did it the same way

因为你想要填写另一张表,我也是这样做的

#include <stdio.h>
#include <string.h>

#define SIDANTAL    7
#define BYTE 4

int main(void)
{
    char alfabet[] = "abcdefghijklmnopqrstuvwxyz .";
    char sequence[SIDANTAL][BYTE * 2 + 1];
    char *end = strstr(alfabet, " .");
    size_t len = end ? (end - alfabet + 2) : strlen(alfabet), cpos = 0;

    for(size_t i = 0; i < SIDANTAL; i++)
    {
        for(size_t k = 0; k < BYTE; k++)
        {
            sequence[i][k * 2] = cpos < len ? alfabet[cpos] : ' ';
            sequence[i][k * 2 + 1] = ' ';
            cpos++;
        }
        sequence[i][BYTE * 2] = 0;
    }

    for(size_t i = 0; i < SIDANTAL; i++)
    {
        printf("%s\n", sequence[i]);
    }
}

#1


1  

Here you have the function. If the string is terminated by the " ." it takes the data up to '.'. If not it takes the data up to number of rows and columns or the end of the string to avoid an UB. If the string is too short the rest of the table is filled with spaces. Of course you can break at this point but i do not know if it is the idea.

在这里你有这个功能。如果字符串被“。”终止。它将数据带到'。'。如果不是,它将数据增加到行数和列数或字符串的结尾以避免UB。如果字符串太短,表的其余部分将填充空格。当然你可以在这一点上打破,但我不知道这是不是这个想法。

Because you wanted to fill another table I did it the same way

因为你想要填写另一张表,我也是这样做的

#include <stdio.h>
#include <string.h>

#define SIDANTAL    7
#define BYTE 4

int main(void)
{
    char alfabet[] = "abcdefghijklmnopqrstuvwxyz .";
    char sequence[SIDANTAL][BYTE * 2 + 1];
    char *end = strstr(alfabet, " .");
    size_t len = end ? (end - alfabet + 2) : strlen(alfabet), cpos = 0;

    for(size_t i = 0; i < SIDANTAL; i++)
    {
        for(size_t k = 0; k < BYTE; k++)
        {
            sequence[i][k * 2] = cpos < len ? alfabet[cpos] : ' ';
            sequence[i][k * 2 + 1] = ' ';
            cpos++;
        }
        sequence[i][BYTE * 2] = 0;
    }

    for(size_t i = 0; i < SIDANTAL; i++)
    {
        printf("%s\n", sequence[i]);
    }
}