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

Loading…
Cancel
Save