You did not specify a programming language, so here are a few C # that perform the requested operation:
int[] maxLengths = new int[100]; string[][] splitLines = new string[input.Length][]; for (int i = 0; i < input.Length; i++) { splitLines[i] = input[i].Split(' '); for (int j = 0; j < splitLines[i].Length; j++) { maxLengths[j] = Math.Max(maxLengths[j], splitLines[i][j].Length); } } for (int i = 0; i < splitLines.Length; i++) { for (int j = 0; j < splitLines[i].Length; j++) { Console.Write(string.Format("0,-" + (maxLengths[j] + 1) + ":G}", splitLines[i][j])); } Console.WriteLine(); }
Note that 100 must be greater than or equal to the number of segments per row. You can make this number not correct with little work if you want.
Basically, this algorithm breaks each line based on spaces, then for each part it calculates the maximum in the total array. Finally, it goes through all these segmented parts and prints them left-aligned using spaces to the largest size (plus 1 to get a space between the elements).
Guvante Jan 10 2018-12-12T00: 00Z
source share