Of course it doesn't print anything.
It's purpose is to compute the grid.
Here's something for you to study.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void trimNewline(char *line) {
size_t len = strlen(line);
if (len && line[len - 1] == '\n')
line[len - 1] = '\0';
}
void inputLine(char *line, size_t max, const char *prompt) {
printf("%s", prompt);
if (fgets(line, max, stdin) == NULL) {
fprintf(stderr, "No line read\n");
exit(EXIT_FAILURE);
}
trimNewline(line);
}
void gridStr(char mat[10][10], size_t columns, const char *line)
{
size_t r = 0, c = 0;
int i = 0;
while (line[i]) {
mat[r][c] = line[i++];
if ( ++c == columns ) {
c = 0;
r++;
}
}
}
void printGrid(const char mat[10][10], size_t rows, size_t columns) {
for ( size_t r = 0 ; r < rows ; r++ ) {
for ( size_t c = 0 ; c < columns ; c++ ) {
putchar(mat[r][c]);
}
putchar('\n');
}
}
int main(){
char str[50];
char key[50];
inputLine(str, sizeof(str), "Please enter string:");
inputLine(key, sizeof(key), "Please enter key:");
size_t columns = strlen(key);
size_t rows = (strlen(str) + columns - 1) / columns;
char mat[10][10] = { { 0 } };
gridStr(mat, columns, str);
printGrid(mat, rows, columns);
return 0;
}
$ gcc foo.c
$ ./a.out
Please enter string:thislongline.
Please enter key:key
thi
slo
ngl
ine
.
$
First, anytime you start copy/pasting more than a few lines of code, you should be making a function out of it.
A note about line 50.
If the length of the line was a simple multiple of the length of the key, then a simple
rows = strlen(str) / columns would suffice.
But integer division rounds down, so if there are any excess characters to complete the last partial row, we need an extra row.