Skip to content

Commit

Permalink
Bad decision of task centerFile.
Browse files Browse the repository at this point in the history
  • Loading branch information
Iksburg committed Nov 14, 2020
1 parent c861ae7 commit c15dbbd
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/lesson7/task1/Files.kt
Original file line number Diff line number Diff line change
Expand Up @@ -168,22 +168,23 @@ fun centerFile(inputName: String, outputName: String) {
var lineCenter: Int
var newString: String
for (line in File(inputName).readLines()) {
if (line.length > maxLineLength) maxLineLength = line.length
if (line.trim().length > maxLineLength) maxLineLength = line.trim().length
}
val maxLineCenter = (maxLineLength - 1) / 2
for (line in File(inputName).readLines()) {
for (i in line.indices) {
if (line[i] != ' ' && firstIndexLine == -1) {
val newLine = line.trim()
for (i in newLine.indices) {
if (newLine[i] != ' ' && firstIndexLine == -1) {
firstIndexLine = i
} else {
lastIndexLine = i
}
}
lineCenter = lastIndexLine - (lastIndexLine - firstIndexLine) / 2
newString = when {
lineCenter == maxLineCenter -> line.trim()
lineCenter > maxLineCenter -> line.drop(firstIndexLine - 1)
else -> line.padStart(line.length + maxLineCenter - lineCenter)
lineCenter == maxLineCenter -> newLine
lineCenter > maxLineCenter -> newLine.drop(firstIndexLine - 1)
else -> newLine.padStart(newLine.length + maxLineCenter - lineCenter)
}
writer.write(newString)
writer.newLine()
Expand Down

0 comments on commit c15dbbd

Please sign in to comment.