Basically i have to design and implement this on groovy , this is to encode and decode a specific paragraph ?
Asked
Active
Viewed 599 times
0
-
what part are you having problems with? – hvgotcodes Jan 06 '11 at 20:04
-
basically im stuck on starting the code just want the structure. – ben Jan 06 '11 at 20:17
-
@ben, then my answer is applicable. – hvgotcodes Jan 06 '11 at 20:21
-
yeah thanks will leave a post if its still giving me problems.. – ben Jan 06 '11 at 20:23
-
just reasearch something ive been stuck on for abit – ben Jan 06 '11 at 20:47
-
@ben, sigh, i edited my answer with enough to really get you started. you can figure out the rest. – hvgotcodes Jan 06 '11 at 21:07
1 Answers
0
You can check out this
http://groovy.codehaus.org/ExpandoMetaClass+-+Dynamic+Method+Names
which shows the typical use case for codecs.
Basically something like (from that link)
class HTMLCodec {
static encode = { theTarget ->
HtmlUtils.htmlEscape(theTarget.toString())
}
static decode = { theTarget ->
HtmlUtils.htmlUnescape(theTarget.toString())
}
}
you wont use the HtmlUtils, but the structure is the same.
EDIT -- here is an example on how to do the substitution. Note this can probably be more groovy, and it doesn't deal with punctuation, but it should help
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)
EDIT -- here is a solution that is a bit more groovy
def plainText = 'hello'
def cipherText = ""
plainText.each {c ->
if (Character.isUpperCase((Character)c))
cipherText += c.toLowerCase()
else
cipherText += c.toUpperCase()
}
println(cipherText)

hvgotcodes
- 118,147
- 33
- 203
- 236
-
Or a bit more groovy: `'hello'.collect { c -> Character.isUpperCase( (Character)c ) ? c.toLowerCase() : c.toUpperCase() }.join( '' )` ;-) – tim_yates Jan 21 '11 at 10:12