Skip to main content
Added the else part of output printing 'if' condition : This now prints properly.
Source Link
  1. Looking at this code:

    if(strTemp.length() > maxWordLen)
        maxWordLen = strTemp.length();
    

    This could be reduced to:

    maxWordLen = Math.max(maxWordLen,strTemp.length());
    
  2. Inline the incrementation:

    charArr[i][j++] = ch;
    

Or just use System.arraycopy:

    char[][] charArr = new char[strArr.length][maxWordLen];
    for (int i = 0; i < strArr.length; i++) {
        System.arraycopy(strArr[i].toCharArray(), 0, charArr[i], 0, strArr[i].length());
    }
  1. Either inline the output:

    System.out.print((i != 0 ? " " : "") + charArr[i][j]);
    

    or at least place braces:

    if (i!=0){
       System.out.print(" ");
    }
    

4 . If you wonder about other solutions, check this one:

    int i = 0;

    boolean hasMoreChars = true;

    while (hasMoreChars) {

        hasMoreChars = false;

        for (String str : strArr) {

            if (i < str.length()) {

                hasMoreChars = true;

                System.out.print(str.charAt(i) + " ");
            }
            else {
                //two spaces, added by xploreraj in Edit
                //in case a shorter word comes before longer word
                //we must compensate the absence of character with space
                System.out.print("  ");
            }
        }
        System.out.println();
        i++;
    }
  1. Looking at this code:

    if(strTemp.length() > maxWordLen)
        maxWordLen = strTemp.length();
    

    This could be reduced to:

    maxWordLen = Math.max(maxWordLen,strTemp.length());
    
  2. Inline the incrementation:

    charArr[i][j++] = ch;
    

Or just use System.arraycopy:

    char[][] charArr = new char[strArr.length][maxWordLen];
    for (int i = 0; i < strArr.length; i++) {
        System.arraycopy(strArr[i].toCharArray(), 0, charArr[i], 0, strArr[i].length());
    }
  1. Either inline the output:

    System.out.print((i != 0 ? " " : "") + charArr[i][j]);
    

    or at least place braces:

    if (i!=0){
       System.out.print(" ");
    }
    

4 . If you wonder about other solutions, check this one:

    int i = 0;

    boolean hasMoreChars = true;

    while (hasMoreChars) {

        hasMoreChars = false;

        for (String str : strArr) {

            if (i < str.length()) {

                hasMoreChars = true;

                System.out.print(str.charAt(i) + " ");
            }
        }
        System.out.println();
        i++;
    }
  1. Looking at this code:

    if(strTemp.length() > maxWordLen)
        maxWordLen = strTemp.length();
    

    This could be reduced to:

    maxWordLen = Math.max(maxWordLen,strTemp.length());
    
  2. Inline the incrementation:

    charArr[i][j++] = ch;
    

Or just use System.arraycopy:

    char[][] charArr = new char[strArr.length][maxWordLen];
    for (int i = 0; i < strArr.length; i++) {
        System.arraycopy(strArr[i].toCharArray(), 0, charArr[i], 0, strArr[i].length());
    }
  1. Either inline the output:

    System.out.print((i != 0 ? " " : "") + charArr[i][j]);
    

    or at least place braces:

    if (i!=0){
       System.out.print(" ");
    }
    

4 . If you wonder about other solutions, check this one:

    int i = 0;

    boolean hasMoreChars = true;

    while (hasMoreChars) {

        hasMoreChars = false;

        for (String str : strArr) {

            if (i < str.length()) {

                hasMoreChars = true;

                System.out.print(str.charAt(i) + " ");
            }
            else {
                //two spaces, added by xploreraj in Edit
                //in case a shorter word comes before longer word
                //we must compensate the absence of character with space
                System.out.print("  ");
            }
        }
        System.out.println();
        i++;
    }
edited body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237
  1. Looking at this code:

    if(strTemp.length() > maxWordLen)
             maxWordLen = strTemp.length();
    

    This could be reduced to:

    maxWordLen = Math.max(maxWordLen,strTemp.length());
    
  2. Inline the incrementation:

    charArr[i][j++] = ch;
    

Or just use System.arraycopy:

    char[][] charArr = new char[strArr.length][maxWordLen];
    for (int i = 0; i < strArr.length; i++) {
        System.arraycopy(strArr[i].toCharArray(), 0, charArr[i], 0, strArr[i].length());
    }
  1. Either inline the output:

    System.out.print((i != 0 ? " " : "") + charArr[i][j]);
    

    or at least place braces:

    if (i!=0){
       System.out.print(" ");
    }
    

4 . If you wonder about other solutions, check this one:

    int i = 0;

    boolean hasMoreChars = true;

    while (hasMoreChars) {

        hasMoreChars = false;

        for (String str : strArr) {

            if (i < str.length()) {

                hasMoreChars = true;

                System.out.print(str.charAt(i) + " ");
            }
        }
        System.out.println();
        i++;
    }
  1. Looking at this code:

    if(strTemp.length() > maxWordLen)
             maxWordLen = strTemp.length();
    

    This could be reduced to:

    maxWordLen = Math.max(maxWordLen,strTemp.length());
    
  2. Inline the incrementation:

    charArr[i][j++] = ch;
    

Or just use System.arraycopy:

    char[][] charArr = new char[strArr.length][maxWordLen];
    for (int i = 0; i < strArr.length; i++) {
        System.arraycopy(strArr[i].toCharArray(), 0, charArr[i], 0, strArr[i].length());
    }
  1. Either inline the output:

    System.out.print((i != 0 ? " " : "") + charArr[i][j]);
    

    or at least place braces:

    if (i!=0){
       System.out.print(" ");
    }
    

4 . If you wonder about other solutions, check this one:

    int i = 0;

    boolean hasMoreChars = true;

    while (hasMoreChars) {

        hasMoreChars = false;

        for (String str : strArr) {

            if (i < str.length()) {

                hasMoreChars = true;

                System.out.print(str.charAt(i) + " ");
            }
        }
    System.out.println();
    i++;
}
  1. Looking at this code:

    if(strTemp.length() > maxWordLen)
        maxWordLen = strTemp.length();
    

    This could be reduced to:

    maxWordLen = Math.max(maxWordLen,strTemp.length());
    
  2. Inline the incrementation:

    charArr[i][j++] = ch;
    

Or just use System.arraycopy:

    char[][] charArr = new char[strArr.length][maxWordLen];
    for (int i = 0; i < strArr.length; i++) {
        System.arraycopy(strArr[i].toCharArray(), 0, charArr[i], 0, strArr[i].length());
    }
  1. Either inline the output:

    System.out.print((i != 0 ? " " : "") + charArr[i][j]);
    

    or at least place braces:

    if (i!=0){
       System.out.print(" ");
    }
    

4 . If you wonder about other solutions, check this one:

    int i = 0;

    boolean hasMoreChars = true;

    while (hasMoreChars) {

        hasMoreChars = false;

        for (String str : strArr) {

            if (i < str.length()) {

                hasMoreChars = true;

                System.out.print(str.charAt(i) + " ");
            }
        }
        System.out.println();
        i++;
    }
added 414 characters in body
Source Link
  1. Looking at this code:

    if(strTemp.length() > maxWordLen)
             maxWordLen = strTemp.length();
    

    This could be reduced to:

    maxWordLen = Math.max(maxWordLen,strTemp.length());
    
  2. Inline the incrementation:

    charArr[i][j++] = ch;
    

Or just use System.arraycopy:

    char[][] charArr = new char[strArr.length][maxWordLen];
    for (int i = 0; i < strArr.length; i++) {
        System.arraycopy(strArr[i].toCharArray(), 0, charArr[i], 0, strArr[i].length());
    }
  1. Either inline the output:

    System.out.print((i != 0 ? " " : "") + charArr[i][j]);
    

    or at least place braces:

    if (i!=0){
       System.out.print(" ");
    }
    

4 . If you wonder about other solutions, check this one:

    int i = 0;

    boolean hasMoreChars = true;

    while (hasMoreChars) {

        hasMoreChars = false;

        for (String str : strArr) {

            if (i < str.length()) {

                hasMoreChars = true;

                System.out.print(str.charAt(i) + " ");
            }
        }
    System.out.println();
    i++;
}
  1. Looking at this code:

    if(strTemp.length() > maxWordLen)
             maxWordLen = strTemp.length();
    

    This could be reduced to:

    maxWordLen = Math.max(maxWordLen,strTemp.length());
    
  2. Inline the incrementation:

    charArr[i][j++] = ch;
    

Or just use System.arraycopy:

    char[][] charArr = new char[strArr.length][maxWordLen];
    for (int i = 0; i < strArr.length; i++) {
        System.arraycopy(strArr[i].toCharArray(), 0, charArr[i], 0, strArr[i].length());
    }
  1. Either inline the output:

    System.out.print((i != 0 ? " " : "") + charArr[i][j]);
    

    or at least place braces:

    if (i!=0){
       System.out.print(" ");
    }
    
  1. Looking at this code:

    if(strTemp.length() > maxWordLen)
             maxWordLen = strTemp.length();
    

    This could be reduced to:

    maxWordLen = Math.max(maxWordLen,strTemp.length());
    
  2. Inline the incrementation:

    charArr[i][j++] = ch;
    

Or just use System.arraycopy:

    char[][] charArr = new char[strArr.length][maxWordLen];
    for (int i = 0; i < strArr.length; i++) {
        System.arraycopy(strArr[i].toCharArray(), 0, charArr[i], 0, strArr[i].length());
    }
  1. Either inline the output:

    System.out.print((i != 0 ? " " : "") + charArr[i][j]);
    

    or at least place braces:

    if (i!=0){
       System.out.print(" ");
    }
    

4 . If you wonder about other solutions, check this one:

    int i = 0;

    boolean hasMoreChars = true;

    while (hasMoreChars) {

        hasMoreChars = false;

        for (String str : strArr) {

            if (i < str.length()) {

                hasMoreChars = true;

                System.out.print(str.charAt(i) + " ");
            }
        }
    System.out.println();
    i++;
}
reformatted code to code. these lists are f*cking everything up
Source Link
Vogel612
  • 25.3k
  • 7
  • 58
  • 141
Loading
added 228 characters in body
Source Link
Loading
Source Link
Loading