'Encryption of text using a matrix

[![скрин][1]][1][scrreen]2I need to give it a look like the one on the screen, but I'm sure I did it wrong. The point is that there is a string str, in this case it is (i love my mother very big and love car) and after entering this string in the matrix you need to divide it into groups of 5 letters. The result should be as follows (irlyo bvieg maynm dolto hveer cvaer).

public static void main(String[] args) {


        String[][] matrixA;
        matrixA = new String[2][15];

        matrixA[0][0] = "i";
        matrixA[0][1] = "l";
        matrixA[0][2] = "o";
        matrixA[0][3] = "v";
        matrixA[0][4] = "e";
        matrixA[0][5] = "m";
        matrixA[0][6] = "y";
        matrixA[0][7] = "m";
        matrixA[0][8] = "o";
        matrixA[0][9] = "t";
        matrixA[0][10] = "h";
        matrixA[0][11] = "e";
        matrixA[0][12] = "r";
        matrixA[0][13] = "v";
        matrixA[0][14] = "e";
        matrixA[1][0] = "r";
        matrixA[1][1] = "y";
        matrixA[1][2] = "b";
        matrixA[1][3] = "i";
        matrixA[1][4] = "g";
        matrixA[1][5] = "a";
        matrixA[1][6] = "n";
        matrixA[1][7] = "d";
        matrixA[1][8] = "l";
        matrixA[1][9] = "o";
        matrixA[1][10] = "v";
        matrixA[1][11] = "e";
        matrixA[1][12] = "c";
        matrixA[1][13] = "a";
        matrixA[1][14] = "r";


        for (int i = 0; i < matrixA.length; i++) {
            for (int j = 0; j < matrixA[i].length; j++) {
                System.out.print(matrixA[i][j] + "\t");
            }
            System.out.println();
        }
    }


Solution 1:[1]

I see at the bottom you have two for-loops that loop over the matrix. All we need to do in order to achieve the algorithm you're asking for is to loop over the matrix but with the dimensions flipped. Then check if it's time to add a space by using modulus. Try out this function, let me know if you have any questions.

public static String getGroups(String[][] matrix, int groupSize) {
    String output = "";
    int matrixWidth = matrix.length;
    int matrixHeight = matrix[0].length;
    
    int k = 0;
    for (int i = 0; i < matrixHeight; i++) {
        for (int j = 0; j < matrixWidth; j++) {
            k++;
            output += matrix[j][i];
            if(k % groupSize == 0) output += " ";
        }
    }
    
    return output;
}

All you need to do is call getGroups, pass in matrixA and pass in a groupSize of 5. This is what it would look like: System.out.println(getGroups(matrixA, 5));

Also a heads up, you're missing a double-quotation mark (") on this line matrixA[0][10] = h";

This means the complete code should look like this:

public static void main(String[] args) {
    String[][] matrixA;
    matrixA = new String[2][15];

    matrixA[0][0] = "i";
    matrixA[0][1] = "l";
    matrixA[0][2] = "o";
    matrixA[0][3] = "v";
    matrixA[0][4] = "e";
    matrixA[0][5] = "m";
    matrixA[0][6] = "y";
    matrixA[0][7] = "m";
    matrixA[0][8] = "o";
    matrixA[0][9] = "t";
    matrixA[0][10] = "h";
    matrixA[0][11] = "e";
    matrixA[0][12] = "r";
    matrixA[0][13] = "v";
    matrixA[0][14] = "e";
    matrixA[1][0] = "r";
    matrixA[1][1] = "y";
    matrixA[1][2] = "b";
    matrixA[1][3] = "i";
    matrixA[1][4] = "g";
    matrixA[1][5] = "a";
    matrixA[1][6] = "n";
    matrixA[1][7] = "d";
    matrixA[1][8] = "l";
    matrixA[1][9] = "o";
    matrixA[1][10] = "v";
    matrixA[1][11] = "e";
    matrixA[1][12] = "c";
    matrixA[1][13] = "a";
    matrixA[1][14] = "r";

    for (int i = 0; i < matrixA.length; i++) {
        for (int j = 0; j < matrixA[i].length; j++) {
            System.out.print(matrixA[i][j] + "\t");
        }
        System.out.println();
    }
    
    System.out.println(getGroups(matrixA, 5));
}

public static String getGroups(String[][] matrix, int groupSize) {
    String output = "";
    int matrixWidth = matrix.length;
    int matrixHeight = matrix[0].length;
    
    int k = 0;
    for (int i = 0; i < matrixHeight; i++) {
        for (int j = 0; j < matrixWidth; j++) {
            k++;
            output += matrix[j][i];
            if(k % groupSize == 0) output += " ";
        }
    }
    
    return output;
}

Solution 2:[2]

@IvanKolisnik, To process the string you specified: i love my mother very big and love car and use the getGroups() method provided by @MorganS42 you would need to do something like this:

Create the Matrix (like the sample you had shown)

String strg = "i love my mother very big and love car";
strg = strg.replaceAll("\\s+", ""); // Remove all whitespaces!
int columns = strg.length() / 2;
String[][] matrix = new String[2][columns];

int indexer = 0;
for (int row = 0; row < matrix.length; row++) {
    for (int column = 0; column < matrix[row].length; column++) {
        matrix[row][column] = Character.toString(strg.charAt(indexer));
        indexer++;
    }
}

// Display Current Generated Matrix...
for (int i = 0; i < matrix.length; i++) {
    for (int j = 0; j < matrix[i].length; j++) {
        if (matrix[i][j].isEmpty()) {
            break;
        }
        System.out.print(matrix[i][j]);
    }
    System.out.println();
}

System.out.println();

Display the encrypted line (as per your specs):

/* Get the desired grouping from the generated 
   matrix and displsy it to console...     */
String encryptedString = getGroups(matrix, 5);
System.out.println(encryptedString);
 

Output to console should be:

ilovemymotherve
rybigandlovecar

irlyo bvieg maynm dolto hveer cvaer

Consequently...if the string to encrypt was:

I love my mother very big but I love my car bigger!

The console output would be:

Ilovemymotherverybig
butIlovemycarbigger!

Ibluo tvIel moyvm eomty hcear rvbei rgygb eirg! 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2