簡體   English   中英

如何為字母替換密碼(常規)創建編碼器和解碼器?

[英]how to create an Encoder and Decoder for the Alphabetic substitution cipher (groovy)?

基本上我必須在groovy上設計和實現,這是對特定段落進行編碼和解碼嗎?

你可以看看這個

http://groovy.codehaus.org/ExpandoMetaClass+-+Dynamic+Method+名稱

它顯示了編解碼器的典型用例。

基本上像(從該鏈接)

class HTMLCodec {
    static encode = { theTarget ->
        HtmlUtils.htmlEscape(theTarget.toString())
    }

    static decode = { theTarget ->
        HtmlUtils.htmlUnescape(theTarget.toString())
    }
}

您不會使用HtmlUtils,但是結構是相同的。

編輯-這是有關如何進行替換的示例。 請注意,這可能更普通,並且不涉及標點符號,但應該會有所幫助

def plainText = 'hello'
def solutionChars = new char[plainText.size()]
for (def i = 0; i < plainText.size(); i++){
        def currentChar = plainText.charAt(i)
        if (Character.isUpperCase(currentChar))
                solutionChars[i] = Character.toLowerCase(currentChar)
        else
                solutionChars[i] = Character.toUpperCase(currentChar)

}

def cipherText = new String(solutionChars)
println(solutionChars)

編輯-這是一個更時髦的解決方案

def plainText = 'hello'
def cipherText = ""
plainText.each {c ->
    if (Character.isUpperCase((Character)c))
        cipherText += c.toLowerCase()
    else
        cipherText += c.toUpperCase()
}

println(cipherText)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM