将一个字符以矩阵形式放入二维数组中

Putting a char in a 2d array in matrix form

我正在尝试制作一个接受字符串和密钥的程序。键确定矩阵中的列数。例如,如果字符串是 hello there 并且键是 BYE,则输出应该是:(_ 表示 space)

h e l
l o _
t h e
r e _

这是我的代码。我无法打印矩阵。

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

int main(){
    char key[50];
    char line[256];
 
    printf("Enter your string:");
    if (fgets(line, sizeof line, stdin) == NULL) {
        fprintf(stderr, "No line read\n");
        exit(EXIT_FAILURE);
    }
    
    printf("Enter your key");
    if (fgets(key, sizeof key, stdin) == NULL) {
        fprintf(stderr, "No line read\n");
        exit(EXIT_FAILURE);
    }
 
    int len = strlen(line);
    if (len && line[len - 1] == '\n')
        line[--len] = '[=11=]';
        
    int len1 = strlen(key);
    if (len1 && key[len1 - 1] == '\n')
        key[--len]= '[=11=]';
        
    printf("%s\n", line);
    printf("%s\n", key);
    //ascendingOrderString(key);
    gridStr(line,key);
}

void gridStr(char *line, char *key)
{    
    char mat[10][10];
    int columns = strlen(key)-1;
    char wordR;
    int rows = 0;
    int i,j = 0;
  
    while (line[i]) {
        putchar(line[i]);
        mat[rows][columns] = line[i++];  
        if (i % columns == 0) putchar('\n');
    }
    if (i % columns != 0)  putchar('\n');

    printf("%s", mat[i][j]);
    }
}

从代码中删除额外的 } 并为 gridStr 添加原型定义后,我看到了以下挑战:

$ gcc -Wall hack.c
hack.c:53:18: warning: format specifies type 'char *' but the argument has type 'char' [-Wformat]
    printf("%s", mat[i][j]);
            ~~   ^~~~~~~~~
            %c
hack.c:42:10: warning: unused variable 'wordR' [-Wunused-variable]
    char wordR;
         ^
hack.c:46:17: warning: variable 'i' is uninitialized when used here [-Wuninitialized]
    while (line[i]) {
                ^
hack.c:44:10: note: initialize the variable 'i' to silence this warning
    int i,j = 0;
         ^
          = 0
3 warnings generated.
$ 

所以,让我们用 %c 替换 %s 并用 0 初始化 i

$ gcc -Wall hack.c
hack.c:42:10: warning: unused variable 'wordR' [-Wunused-variable]
    char wordR;
         ^
1 warning generated.
$ ./a.out         
Enter your string:hello there
Enter your keyBYE
hello there
BYE

hel
lo 
the
re
u%                                                                                                                                                          
$ 

看起来我们快到了。 % 符号表示在应用程序末尾没有打印换行符。前面的 'u' 表示由于 i 递增而打印未初始化的值。

mat[rows][columns] = line[i++]; 总是更新同一个元素。这当然不是故意的。

删除了wordR,输出'_'而不是'',填充矩阵:

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

void gridStr(char *line, char *key);

int main(void) {
    char key[50];
    char line[256];
    
    printf("Enter your string: ");
    if (fgets(line, sizeof line, stdin) == NULL) {
        fprintf(stderr, "No line read\n");
        exit(EXIT_FAILURE);
    }
    
    printf("Enter your key: ");
    if (fgets(key, sizeof key, stdin) == NULL) {
        fprintf(stderr, "No line read\n");
        exit(EXIT_FAILURE);
    }
    
    int len = strlen(line);
    if (len && line[len - 1] == '\n')
        line[--len] = '[=12=]';
        
    int len1 = strlen(key);
    if (len1 && key[len1 - 1] == '\n')
        key[--len]= '[=12=]';
        
    printf("%s\n", line);
    printf("%s\n", key);
    //ascendingOrderString(key);
    gridStr(line, key);
}
        
void gridStr(char *line, char *key)
{    
    char mat[10][10] = {0};
    int columns = strlen(key)-1;
    int rows = 0;
    int i=0,j = 0;
    
    while (line[i]) {
        if (line[i] == ' ') {
            putchar('_');
        } else {
            putchar(line[i]);
        }
        mat[rows][i % columns] = line[i];
        i++;
        if (i > 0 && i % columns == 0) {
            putchar('\n');
            rows++;
        }
    }
    if (i % columns != 0)  putchar('\n');

    rows++; // from current row to number of rows
    printf("\nMatrix:\n");

    for (i = 0; i < rows; i++) {
        for (j = 0; j < columns; j++) {
            if (mat[i][j] == ' ') {
                putchar('_');
            } else {
                putchar(mat[i][j]);
            }
        }
        printf("\n");
    }
}

我们开始:

$ gcc -Wall hack.c
$ ./a.out
Enter your string: hello there
Enter your key: BYE
hello there
BYE

hel
lo_
the
re

Matrix:
hel
lo_
the
re
$ 

直接打印输出和矩阵输出匹配。完成。