Skip to main content
Commonmark migration
Source Link

#Variable Names

Variable Names

Your variables names are unnecessarily abbreviated and genetic. Also suffixes such as Arr are usually unnecessary. Here some examples I'd use:

  • str => sentence
  • strArr => words
  • strTemp => word

#Algorithm

Algorithm

You could drop copying the strings into the character array, by using strArr directly and simply checking if j exceeds the length of the word and printing a space if it does.

Also getting the maximal word length could also be avoided, if you check during output, if all strings are shorter than j.

#My version

My version

public static void toVerticalWords(String sentence){
    String[] words = sentence.split("\\s");
    
    boolean allWordsEnded;
    int row = 0;
    
    do {
        allWordsEnded = true;

        char[] output = new char[words.length * 2];
        int column = 0;
        for (String word : words) {
            char c;
            if (row < word.length()) {
                c = word.charAt(row);
                allWordsEnded = false;
            } else {
                c = ' ';
            }
            output[column++] = c;
            output[column++] = ' ';
        }
        if (!allWordsEnded) {
            System.out.println(output);
        }
        row++;
    }
    while (!allWordsEnded);
}

The main loop is a bit more complicated, because I cache the line to be written, so when all words have ended, it won't output a line of spaces.

#Variable Names

Your variables names are unnecessarily abbreviated and genetic. Also suffixes such as Arr are usually unnecessary. Here some examples I'd use:

  • str => sentence
  • strArr => words
  • strTemp => word

#Algorithm

You could drop copying the strings into the character array, by using strArr directly and simply checking if j exceeds the length of the word and printing a space if it does.

Also getting the maximal word length could also be avoided, if you check during output, if all strings are shorter than j.

#My version

public static void toVerticalWords(String sentence){
    String[] words = sentence.split("\\s");
    
    boolean allWordsEnded;
    int row = 0;
    
    do {
        allWordsEnded = true;

        char[] output = new char[words.length * 2];
        int column = 0;
        for (String word : words) {
            char c;
            if (row < word.length()) {
                c = word.charAt(row);
                allWordsEnded = false;
            } else {
                c = ' ';
            }
            output[column++] = c;
            output[column++] = ' ';
        }
        if (!allWordsEnded) {
            System.out.println(output);
        }
        row++;
    }
    while (!allWordsEnded);
}

The main loop is a bit more complicated, because I cache the line to be written, so when all words have ended, it won't output a line of spaces.

Variable Names

Your variables names are unnecessarily abbreviated and genetic. Also suffixes such as Arr are usually unnecessary. Here some examples I'd use:

  • str => sentence
  • strArr => words
  • strTemp => word

Algorithm

You could drop copying the strings into the character array, by using strArr directly and simply checking if j exceeds the length of the word and printing a space if it does.

Also getting the maximal word length could also be avoided, if you check during output, if all strings are shorter than j.

My version

public static void toVerticalWords(String sentence){
    String[] words = sentence.split("\\s");
    
    boolean allWordsEnded;
    int row = 0;
    
    do {
        allWordsEnded = true;

        char[] output = new char[words.length * 2];
        int column = 0;
        for (String word : words) {
            char c;
            if (row < word.length()) {
                c = word.charAt(row);
                allWordsEnded = false;
            } else {
                c = ' ';
            }
            output[column++] = c;
            output[column++] = ' ';
        }
        if (!allWordsEnded) {
            System.out.println(output);
        }
        row++;
    }
    while (!allWordsEnded);
}

The main loop is a bit more complicated, because I cache the line to be written, so when all words have ended, it won't output a line of spaces.

Confused rows and cloumns
Source Link
RoToRa
  • 11.4k
  • 1
  • 23
  • 51

#Variable Names

Your variables names are unnecessarily abbreviated and genetic. Also suffixes such as Arr are usually unnecessary. Here some exampleexamples I'd use:

  • str => sentence
  • strArr => words
  • strTemp => word

#Algorithm

You could drop copying the strings into the character array, by using strArr directly and simply checking if j exceeds the length of the word and printing a space if it does.

Also getting the maximal word length could also be avoided, if you check during output, iffif all strings are shorter than j.

#My version

public static void toVerticalWords(String sentence){
    String[] words = sentence.split("\\s");
    
    boolean allWordsEnded;
    int columnrow = 0;
    
    do {
        allWordsEnded = true;

        char[] output = new char[words.length * 2];
        int rowcolumn = 0;
        for (String word : words) {
            char c;
            if (columnrow < word.length()) {
                c = word.charAt(columnrow);
                allWordsEnded = false;
            } else {
                c = ' ';
            }
            output[row]output[column++] = c;
            output[row + 1]output[column++] = ' ';
            row += 2;
        }
        if (!allWordsEnded) {
            System.out.println(output);
        }
        column++;row++;
    }
    while (!allWordsEnded);
}

The main loop is a bit more complicated, because I cache the line to be written, so when all words have ended, it won't output a line of spaces.

#Variable Names

Your variables names are unnecessarily abbreviated and genetic. Also suffixes such as Arr are usually unnecessary. Here some example I'd use:

  • str => sentence
  • strArr => words
  • strTemp => word

#Algorithm

You could drop copying the strings into the character array, by using strArr directly and simply checking if j exceeds the length of the word and printing a space if it does.

Also getting the maximal word length could also be avoided, if you check during output, iff all strings are shorter than j.

#My version

public static void toVerticalWords(String sentence){
    String[] words = sentence.split("\\s");
    
    boolean allWordsEnded;
    int column = 0;
    
    do {
        allWordsEnded = true;

        char[] output = new char[words.length * 2];
        int row = 0;
        for (String word : words) {
            char c;
            if (column < word.length()) {
                c = word.charAt(column);
                allWordsEnded = false;
            } else {
                c = ' ';
            }
            output[row] = c;
            output[row + 1] = ' ';
            row += 2;
        }
        if (!allWordsEnded) {
            System.out.println(output);
        }
        column++;
    }
    while (!allWordsEnded);
}

The main loop is a bit more complicated, because I cache the line to be written, so when all words have ended, it won't output a line of spaces.

#Variable Names

Your variables names are unnecessarily abbreviated and genetic. Also suffixes such as Arr are usually unnecessary. Here some examples I'd use:

  • str => sentence
  • strArr => words
  • strTemp => word

#Algorithm

You could drop copying the strings into the character array, by using strArr directly and simply checking if j exceeds the length of the word and printing a space if it does.

Also getting the maximal word length could also be avoided, if you check during output, if all strings are shorter than j.

#My version

public static void toVerticalWords(String sentence){
    String[] words = sentence.split("\\s");
    
    boolean allWordsEnded;
    int row = 0;
    
    do {
        allWordsEnded = true;

        char[] output = new char[words.length * 2];
        int column = 0;
        for (String word : words) {
            char c;
            if (row < word.length()) {
                c = word.charAt(row);
                allWordsEnded = false;
            } else {
                c = ' ';
            }
            output[column++] = c;
            output[column++] = ' ';
        }
        if (!allWordsEnded) {
            System.out.println(output);
        }
        row++;
    }
    while (!allWordsEnded);
}

The main loop is a bit more complicated, because I cache the line to be written, so when all words have ended, it won't output a line of spaces.

added 860 characters in body
Source Link
RoToRa
  • 11.4k
  • 1
  • 23
  • 51

#Variable Names

Your variables names are unnecessarily abbreviated and genetic. Also suffixes such as Arr are usually unnecessary. Here some example I'd use:

  • str => sentence
  • strArr => words
  • strTemp => word

#Algorithm

You could drop copying the strings into the character array, by using strArr directly and simply checking if j exceeds the length of the word and printing a space if it does.

Also getting the maximal word length could also be avoided, if you check during output, iff all strings are shorter than j.

#My version

public static void toVerticalWords(String sentence){
    String[] words = sentence.split("\\s");
    
    boolean allWordsEnded;
    int column = 0;
    
    do {
        allWordsEnded = true;

        char[] output = new char[words.length * 2];
        int row = 0;
        for (String word : words) {
            char c;
            if (column < word.length()) {
                c = word.charAt(column);
                allWordsEnded = false;
            } else {
                c = ' ';
            }
            output[row] = c;
            output[row + 1] = ' ';
            row += 2;
        }
        if (!allWordsEnded) {
            System.out.println(output);
        }
        column++;
    }
    while (!allWordsEnded);
}

The main loop is a bit more complicated, because I cache the line to be written, so when all words have ended, it won't output a line of spaces.

#Variable Names

Your variables names are unnecessarily abbreviated and genetic. Also suffixes such as Arr are usually unnecessary. Here some example I'd use:

  • str => sentence
  • strArr => words
  • strTemp => word

#Algorithm

You could drop copying the strings into the character array, by using strArr directly and simply checking if j exceeds the length of the word and printing a space if it does.

Also getting the maximal word length could also be avoided, if you check during output, iff all strings are shorter than j.

#Variable Names

Your variables names are unnecessarily abbreviated and genetic. Also suffixes such as Arr are usually unnecessary. Here some example I'd use:

  • str => sentence
  • strArr => words
  • strTemp => word

#Algorithm

You could drop copying the strings into the character array, by using strArr directly and simply checking if j exceeds the length of the word and printing a space if it does.

Also getting the maximal word length could also be avoided, if you check during output, iff all strings are shorter than j.

#My version

public static void toVerticalWords(String sentence){
    String[] words = sentence.split("\\s");
    
    boolean allWordsEnded;
    int column = 0;
    
    do {
        allWordsEnded = true;

        char[] output = new char[words.length * 2];
        int row = 0;
        for (String word : words) {
            char c;
            if (column < word.length()) {
                c = word.charAt(column);
                allWordsEnded = false;
            } else {
                c = ' ';
            }
            output[row] = c;
            output[row + 1] = ' ';
            row += 2;
        }
        if (!allWordsEnded) {
            System.out.println(output);
        }
        column++;
    }
    while (!allWordsEnded);
}

The main loop is a bit more complicated, because I cache the line to be written, so when all words have ended, it won't output a line of spaces.

Source Link
RoToRa
  • 11.4k
  • 1
  • 23
  • 51
Loading