Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static String makeCoding(String key, String word) {
- char[] arrOfKey = new char[key.length()];
- int[] arrOfIndex = new int[key.length()];
- for (int i = 0; i < arrOfKey.length; i++) {
- arrOfKey[i] = key.charAt(i);
- arrOfIndex[i] = i;
- }
- for (int i = 0; i < arrOfKey.length - 1; i++) {
- for (int j = 0; j < arrOfKey.length - 1 - i; j++) {
- if (arrOfKey[j] > arrOfKey[j + 1]) {
- char tempChar = arrOfKey[j];
- arrOfKey[j] = arrOfKey[j + 1];
- arrOfKey[j + 1] = tempChar;
- int tempIndex = arrOfIndex[j];
- arrOfIndex[j] = arrOfIndex[j + 1];
- arrOfIndex[j + 1] = tempIndex;
- }
- }
- }
- char[][] matrix = new char[word.length() / key.length() + 1][key.length()]; // двумерный массив
- int counter = 0;
- for(int i = 0; i < matrix.length; i++) {
- for(int j = 0; j < matrix[i].length; j++) {
- if(counter < word.length()) {
- matrix[i][j] = word.charAt(counter);
- counter++;
- }
- else {
- matrix[i][j] = '0';
- }
- }
- }
- String outputstring = "";
- for (int col = 0; col < matrix[0].length; col++) {
- for (int row = 0; row < matrix.length; row++) {
- if(!(matrix[row][col] == '0')) {
- outputstring += matrix[row][col];
- }
- }
- }
- return outputstring;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement