Advertisement
dxvmxnd

Untitled

Feb 20th, 2025
5
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. public static String makeCoding(String key, String word) {
  2. char[] arrOfKey = new char[key.length()];
  3. int[] arrOfIndex = new int[key.length()];
  4.  
  5. for (int i = 0; i < arrOfKey.length; i++) {
  6. arrOfKey[i] = key.charAt(i);
  7. arrOfIndex[i] = i;
  8. }
  9.  
  10. for (int i = 0; i < arrOfKey.length - 1; i++) {
  11. for (int j = 0; j < arrOfKey.length - 1 - i; j++) {
  12. if (arrOfKey[j] > arrOfKey[j + 1]) {
  13.  
  14. char tempChar = arrOfKey[j];
  15. arrOfKey[j] = arrOfKey[j + 1];
  16. arrOfKey[j + 1] = tempChar;
  17.  
  18. int tempIndex = arrOfIndex[j];
  19. arrOfIndex[j] = arrOfIndex[j + 1];
  20. arrOfIndex[j + 1] = tempIndex;
  21. }
  22. }
  23. }
  24.  
  25. char[][] matrix = new char[word.length() / key.length() + 1][key.length()]; // двумерный массив
  26. int counter = 0;
  27.  
  28. for(int i = 0; i < matrix.length; i++) {
  29. for(int j = 0; j < matrix[i].length; j++) {
  30. if(counter < word.length()) {
  31. matrix[i][j] = word.charAt(counter);
  32. counter++;
  33. }
  34. else {
  35. matrix[i][j] = '0';
  36. }
  37. }
  38. }
  39.  
  40.  
  41. String outputstring = "";
  42. for (int col = 0; col < matrix[0].length; col++) {
  43. for (int row = 0; row < matrix.length; row++) {
  44. if(!(matrix[row][col] == '0')) {
  45. outputstring += matrix[row][col];
  46. }
  47. }
  48. }
  49.  
  50. return outputstring;
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement