How to accept text and put each letter into a 2d matrix?

Joined
Jun 3, 2022
Messages
1
Reaction score
0
So basically this takes in a string and key. The length of the key determines the amount of columns in the matrix. However, my code doesn't seem to work.


Code:
#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);

    gridStr(line,key);
}

void gridStr(char *line, char *key)
{   
    int mat[10][10];
    int j=0;
    int ctr=j;
            printf("The matrix is:\n");
            for(i=0;i<strlen(line);i++)
            {
                        for(j=j;ctr<strlen(key);j++)
                        {
                                    if(line[j]!='\0')
                                    {
                                                mat[i][ctr]=line[j];
                                                printf("%c",mat[i][j]);
                                                ctr++;
                                    }
                                    else
                                    {
                                                
                                    }
                        }
                        
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top