Advertisement
peter2396

words

Jan 27th, 2020
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. public class Words
  2. {
  3. static String[] words = {"peter", "mrK"};
  4. static String[][] scramble = new String[words.length][getCols()-longestWord(words)];
  5. static int col;
  6. public static void main(String[] args)
  7. {
  8. fillArray(scramble);
  9. int currentWord = 0;
  10. for (String word: words)
  11. {
  12. col = 0;
  13. for (int i = 0; i<word.length(); i++)
  14. {
  15. for (int n = 0; n<word.length(); n++)
  16. if (word.length()-i > n) {
  17. scramble[currentWord][col] = word.substring(n, word.length()-i);
  18. col++;
  19. }
  20. }
  21. currentWord++;
  22. }
  23. printGrid(scramble);
  24. }
  25.  
  26.  
  27. public static int getCols()
  28. {
  29. int cols = 0;
  30. for (String word: words)
  31. {
  32. for (int i = 0; i<word.length(); i++)
  33. {
  34. for (int n = 0; n<word.length(); n++)
  35. if (word.length()-i > n)
  36. cols++;
  37. }
  38. }
  39. return cols;
  40. }
  41. public static int longestWord(String[] wordArray)
  42. {
  43. String longWord = "";
  44. for (String word: wordArray)
  45. if (word.length() > longWord.length())
  46. longWord = word;
  47. return longWord.length();
  48. }
  49. public static void fillArray(String[][] grid)
  50. {
  51. for (int r = 0; r<grid.length; r++)
  52. for (int c = 0; c<grid[0].length; c++)
  53. grid[r][c] = "*";
  54. }
  55. public static void printGrid(String[][] grid)
  56. {
  57. for (String[] r: grid)
  58. {
  59. String row = "";
  60. for (String element: r)
  61. {
  62. if (!element.equals("*"))
  63. row+=element+" ";
  64. }
  65. System.out.println(row);
  66. }
  67.  
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement