parent
1445e1ef69
commit
8eba596d6e
@ -0,0 +1,88 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
// Function to encode characters into their respective number pairs
|
||||
func encodeChar(char rune) int {
|
||||
// Define the alphabet and numbers with a leading space
|
||||
alphabet := " ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||
|
||||
// Find the index of the character
|
||||
index := strings.IndexRune(alphabet, char)
|
||||
if index == -1 {
|
||||
return -1 // Return -1 if character is not found
|
||||
}
|
||||
return index
|
||||
}
|
||||
|
||||
// Function to apply shift and wrap around
|
||||
func applyShift(value, shift int) int {
|
||||
shifted := (value + shift) % 37
|
||||
if shifted < 0 {
|
||||
shifted += 37
|
||||
}
|
||||
return shifted
|
||||
}
|
||||
|
||||
// Function to format the output based on group size
|
||||
func formatOutput(encoded []int, groupSize int) string {
|
||||
var output strings.Builder
|
||||
for _, num := range encoded {
|
||||
output.WriteString(fmt.Sprintf("%02d", num))
|
||||
}
|
||||
|
||||
// Apply grouping
|
||||
finalOutput := output.String()
|
||||
if groupSize > 0 {
|
||||
var groupedOutput strings.Builder
|
||||
for i := 0; i < len(finalOutput); i += groupSize {
|
||||
if i+groupSize < len(finalOutput) {
|
||||
groupedOutput.WriteString(finalOutput[i:i+groupSize] + " ")
|
||||
} else {
|
||||
groupedOutput.WriteString(finalOutput[i:])
|
||||
}
|
||||
}
|
||||
return strings.TrimSpace(groupedOutput.String())
|
||||
}
|
||||
|
||||
return finalOutput
|
||||
}
|
||||
|
||||
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")
|
||||
flag.Parse()
|
||||
|
||||
// Read input from stdin
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
input, _ := reader.ReadString('\n')
|
||||
input = strings.TrimSpace(input)
|
||||
|
||||
var encoded []int
|
||||
|
||||
// Encode the input string
|
||||
for _, char := range input {
|
||||
if !unicode.IsLetter(char) && !unicode.IsDigit(char) && char != ' ' {
|
||||
continue // Skip characters not in the defined alphabet and numbers
|
||||
}
|
||||
encodedValue := encodeChar(unicode.ToUpper(char))
|
||||
if encodedValue == -1 {
|
||||
continue // Skip characters not found in the alphabet string
|
||||
}
|
||||
shiftedValue := applyShift(encodedValue, *shift)
|
||||
encoded = append(encoded, shiftedValue)
|
||||
}
|
||||
|
||||
// Format the output
|
||||
formattedOutput := formatOutput(encoded, *groupSize)
|
||||
fmt.Println(formattedOutput)
|
||||
}
|
||||
|
Loading…
Reference in new issue