繁体   English   中英

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

[英]Putting a char in a 2d array in matrix form

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

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] = '\0';
        
    int len1 = strlen(key);
    if (len1 && key[len1 - 1] == '\n')
        key[--len]= '\0';
        
    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] = '\0';
        
    int len1 = strlen(key);
    if (len1 && key[len1 - 1] == '\n')
        key[--len]= '\0';
        
    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
$ 

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM