Computer Science, asked by debasmita89, 10 months ago

take a word and store the chara
cter in an array and print it vertically

Answers

Answered by harshhvt
1

public static void toVerticalWords(String str){

       //split the words by whitespace

       String[] strArr = str.split("\\s");

       int maxWordLen = 0;

       //get the longest word length

       for(String strTemp : strArr) {

           if(strTemp.length() > maxWordLen)

               maxWordLen = strTemp.length();

       }

       //make a matrix of the words with each character in an array block

       char[][] charArr = new char[strArr.length][maxWordLen];

       for(int i=0; i<strArr.length; i++) {

           int j=0;

           for(char ch : strArr[i].toCharArray()){

               charArr[i][j] = ch;

               j++;

           }

       }

       //print the vertical word pattern, or transpose of above matrix (2D array)

       for(int j=0; j<maxWordLen; j++) {

           for(int i=0; i<strArr.length; i++) {

               if (i!=0)

                   System.out.print(" ");

               System.out.print(charArr[i][j]);

           }

           System.out.println();

       }

   }Answer:

Explanation:

Similar questions