Advertisement
cogier

Align columns

Apr 7th, 2020
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.99 KB | None | 0 0
  1. 'https://rosettacode.org/wiki/Align_columns
  2. Imports System
  3. Module Program
  4.     Sub Main()
  5.         Dim siCount, siCounter, siLength As Integer
  6.         Dim siLongest As Short = -1
  7.         Dim sRows As New List(Of String)
  8.         Dim sLine As New List(Of String)
  9.         Dim sTemp As String = ""
  10.         Dim sAlign As String = ""
  11.         Dim sInput As String = "Given$a$text$file$of$many$lines, $where$fields$within$a$line$" & "\n" &
  12.         "are$delineated$by$a$single$ 'dollar'$character,$write$a$program" & "\n" &
  13.         "that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$" & "\n" &
  14.         "column$are$separated$by$at$least$one$space." & "\n" &
  15.         "Further, $allow$for$each$word$in$a$column$to$be$either$left$" & "\n" &
  16.         "justified, $right$justified, $or$center$justified$within$its$column."
  17.  
  18.         For Each sTemp In Split(sInput, "\n")
  19.             sLine.Add(sTemp)
  20.         Next
  21.  
  22.         For siCount = 0 To sLine.Count - 1
  23.             For Each sTemp In Split(sLine(siCount), "$")
  24.                 siLength = Len(sTemp)
  25.                 If siLength > siLongest Then siLongest = siLength
  26.                 sRows.Add(Trim(sTemp))
  27.             Next
  28.             sRows.Add("\n")
  29.         Next
  30.  
  31.         For siCounter = 0 To 2
  32.             For Each sTemp In sRows
  33.                 If sTemp = "\n" Then
  34.                     Console.WriteLine()
  35.                     Continue For
  36.                 End If
  37.                 If siCounter = 0 Then Console.Write(sTemp & Space(siLongest - Len(sTemp)))
  38.                 If siCounter = 1 Then Console.Write(Space(siLongest - Len(sTemp)) & sTemp)
  39.                 If siCounter = 2 Then
  40.                     siCount = Math.Floor((siLongest - Len(sTemp)) / 2)
  41.                     sAlign = Space(siCount) & sTemp & Space(siCount)
  42.                     If Len(sAlign) < siLongest Then sAlign &= " "
  43.                     Console.Write(sAlign)
  44.                 End If
  45.             Next
  46.             Console.WriteLine()
  47.         Next
  48.  
  49.     End Sub
  50. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement