C - 打印2D字符阵列

时间:2016-03-11 09:23:45

标签: c arrays char

如何在C中打印2D字符数组的元素?

这是我目前的代码:

int main()
{
  unsigned int size;

  printf("Enter size:\n");
  scanf("%d",&size);

  char word[size][size];

  //Enter the matrix
  for(int k = 0; k < (size); ++k){
    for (int j = 0; j < (size); ++j){
      printf("Enter letter:");
      scanf("%c",&word[k][j]);
    }
  }

  //printf("\n");
  for (int k = 0; k < size; ++k){
    for(int j = 0; j < size; ++j){

      printf("%c",word[k][j]);
    }
    //printf("\n ");
  }
  printf("\n");
}

执行时,它会成对返回元素(使用4x4数组) 例如:

ab
cd
ef
gh
ij
kl
mn
op

而不是我想要的输出:

abcd
efgh
ijkl
mnop

为什么会这样?

5 个答案:

答案 0 :(得分:3)

更改您的scanf解决了所有问题

scanf(" %c",&word[k][j]);  // notice the space before '%c'

您还需要将打印循环更改为

for (k = 0; k < size; ++k){
    for(j = 0; j < size; ++j){
        printf("%c",word[k][j]);
    }
    printf("\n");
}

答案 1 :(得分:1)

我删除了阅读,看起来好像打印:

int main()
{
    const unsigned int size = 4;
    char word[size][size];

    //Enter the matrix
    for (int k = 0; k < (size); ++k) {
        for (int j = 0; j < (size); ++j) {
            word[k][j] = 'a' + j + (k * size);
        }
    }

    for (int k = 0; k < size; ++k) {
        for (int j = 0; j < size; ++j) {

            printf("%c", word[k][j]);
        }
        printf("\n");
    }
    printf("\n");

    getchar();
    return 0;
}

输出:

abcd
efgh
ijkl
mnop

答案 2 :(得分:1)

我发现您的来源存在两个问题。

一个是内存分配 - 实际上并非ansi-c。

如果需要动态内存,则需要在运行时分配它。考虑切换到c ++,因为有标准的设施以更安全的方式帮助解决这个问题。

第二个问题是缓冲区中有一个用作输入字符的空白字符。我想你想清除它。

以下是包含其他评论的来源:

#include <stdio.h>
#include <stdlib.h>

void ansiC()
{
    unsigned int size;

    printf("Enter size:\n");
    scanf("%d", &size);

    //char word[size][size]; <- this is not ansi-c because size is unknown at compile time
    char * word = (char*)malloc(sizeof(char)* size * size);


    //Enter the matrix
    for (int k = 0; k < (size); ++k)
    {
        for (int j = 0; j < (size); ++j)
        {
            printf("Enter letter:");
            scanf("%c ", &word[k * size + j]);
            //since word is just a pointer i changed the way the position is calculated
            //after the character the user presses the enter key
            //this puts a whitespace character on the buffer. 
            //by adding the space after %c you also clear that from the buffer
        }
    }

    //printf("\n");
    for (int k = 0; k < size; ++k)
    {
        for (int j = 0; j < size; ++j)
        {

            printf("%c", word[k * size + j]);
            //since word is just a pointer i changed the way the position is calculated
        }
        //printf("\n ");
    }
    printf("\n");

    free(word); //if you use malloc you need to remember to use free
}

int main()
{
    ansiC();
    return 0;
}

答案 3 :(得分:0)

注意:%c%1s执行不同的操作(除了为后者添加终止空值):

  • c将每个字符包括空格,制表符,cr和lf
  • %1s跳过所有空格(空格,制表符,cr,lf等)

所以在输入时,你应该使用:

char c[2]; // provide room for a terminating null...
...
for(int k = 0; k < (size); ++k){
    for (int j = 0; j < (size); ++j){
        printf("Enter letter:");
        scanf("%1s",c);
        word[k][j] = c[0];
    }
}

在印刷时间:

for (int k = 0; k < size; ++k){
    for(int j = 0; j < size; ++j){
        printf("%c",word[k][j]);
    }
    printf("\n "); // new line after each line
}

答案 4 :(得分:0)

请检查一下。

# include <iostream.h>
# include <conio.h>


void main()
{
clrscr();
char arr[5][3]={"abc","aks","tny","dkn","kbf"};
    for(int a=0;a<5;a++)
    {
        for(int b=0;b<3;b++)
        {
            cout<<" "<<arr[a][b];
        }
        cout<<endl;
    }


 getch();
}