[英]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.