So basically now the code collects the string, key and generates a matrix with column size based on key size. Also it prints the key in alphabetical order. Question is for columnar transposition say the key is "bac" , 'a' is the first character alphabetically so the second column is put first. How do I implement this?

insert
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);
    ascendingOrderString(key);
    gridStr(line,key);
}


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


void ascendingOrderString(char *key) {
    char temp;
    int i, j;
    int n = strlen(key);
    for (i = 0; i < n-1; i++) {
        for (j = i+1; j < n; j++) {
            if (key[i] > key[j]) {
                    temp = key[i];
                    key[i] = key[j];
                    key[j] = temp;
            }
        }
    }


    printf("The sorted string is : %s \n", key);
}