Home > Back-end >  Putting a char in a 2d array in matrix form
Putting a char in a 2d array in matrix form

Time:06-05

I'm trying to make a program which accepts a string and key. The key determines the number of columns in the matrix. Example if the string is hello there and the key is BYE the output should be: (_ represents the space)

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

Here is my code. I can't get the matrix to print.

#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]);
    }
}

CodePudding user response:

After removing the additional } from code and adding a prototype definition for gridStr I see the following challenges:

$ 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.
$ 

So, let's replace %s with %c and initialize i with 0.

$ 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%                                                                                                                                                          
$ 

Looks like we are almost there. The % symbol indicates that there is no newline printed at the end of the application. The 'u' before it indicates the printing of an uninitialized value due to i being incremented.

mat[rows][columns] = line[i ]; updates always the same element. This is certainly not intentional.

Removed wordR, output '_' instead of ' ', fill matrix:

#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");
    }
}

And here we go:

$ 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
$ 

Directly printed output and matrix output match. Done.

  •  Tags:  
  • c
  • Related