Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Words
- {
- static String[] words = {"peter", "mrK"};
- static String[][] scramble = new String[words.length][getCols()-longestWord(words)];
- static int col;
- public static void main(String[] args)
- {
- fillArray(scramble);
- int currentWord = 0;
- for (String word: words)
- {
- col = 0;
- for (int i = 0; i<word.length(); i++)
- {
- for (int n = 0; n<word.length(); n++)
- if (word.length()-i > n) {
- scramble[currentWord][col] = word.substring(n, word.length()-i);
- col++;
- }
- }
- currentWord++;
- }
- printGrid(scramble);
- }
- public static int getCols()
- {
- int cols = 0;
- for (String word: words)
- {
- for (int i = 0; i<word.length(); i++)
- {
- for (int n = 0; n<word.length(); n++)
- if (word.length()-i > n)
- cols++;
- }
- }
- return cols;
- }
- public static int longestWord(String[] wordArray)
- {
- String longWord = "";
- for (String word: wordArray)
- if (word.length() > longWord.length())
- longWord = word;
- return longWord.length();
- }
- public static void fillArray(String[][] grid)
- {
- for (int r = 0; r<grid.length; r++)
- for (int c = 0; c<grid[0].length; c++)
- grid[r][c] = "*";
- }
- public static void printGrid(String[][] grid)
- {
- for (String[] r: grid)
- {
- String row = "";
- for (String element: r)
- {
- if (!element.equals("*"))
- row+=element+" ";
- }
- System.out.println(row);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement