Added a function to make output multiline

master
Aaron Johnon 1 year ago
parent 8eba596d6e
commit d98cdf5303

@ -31,8 +31,8 @@ func applyShift(value, shift int) int {
return shifted
}
// Function to format the output based on group size
func formatOutput(encoded []int, groupSize int) string {
// Function to format the output based on group size and line size
func formatOutput(encoded []int, groupSize int, lineSize int) string {
var output strings.Builder
for _, num := range encoded {
output.WriteString(fmt.Sprintf("%02d", num))
@ -40,25 +40,34 @@ func formatOutput(encoded []int, groupSize int) string {
// Apply grouping
finalOutput := output.String()
if groupSize > 0 {
var groupedOutput strings.Builder
counter := 0
groupCounter := 0
for i := 0; i < len(finalOutput); i += groupSize {
if i+groupSize < len(finalOutput) {
groupedOutput.WriteString(finalOutput[i:i+groupSize] + " ")
} else {
groupedOutput.WriteString(finalOutput[i:])
}
counter++
groupCounter++
// Check if we reached the line size
if lineSize > 0 && groupCounter >= lineSize && i+groupSize < len(finalOutput) {
groupedOutput.WriteString("\n")
groupCounter = 0
}
return strings.TrimSpace(groupedOutput.String())
}
return finalOutput
return strings.TrimSpace(groupedOutput.String())
}
func main() {
// Define command-line flags
shift := flag.Int("s", 0, "Shift amount for the cipher")
groupSize := flag.Int("g", 0, "Group size for output formatting")
lineSize := flag.Int("l", 0, "Line size by number of groups")
flag.Parse()
// Read input from stdin
@ -82,7 +91,7 @@ func main() {
}
// Format the output
formattedOutput := formatOutput(encoded, *groupSize)
formattedOutput := formatOutput(encoded, *groupSize, *lineSize)
fmt.Println(formattedOutput)
}

Loading…
Cancel
Save