0

Basically i have to design and implement this on groovy , this is to encode and decode a specific paragraph ?

ben
  • 1
  • 1

1 Answers1

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