Groovy web console

subscribe to the feed Subscribe
to this
site

some_weird_question

Published 2 months ago by pgupta with tags fb
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
String sent = "Merry went to school this Wednesday"
String[] words = sent.split(' ')
println "input sentence >> "+words
String newSent = ""
words.eachWithIndex { word, idx ->
    if ((idx + 1) % 2 == 0) {

        String s = word
        String n = ""
        char[] arr = s.toCharArray()
        int l2 = arr.length / 2
        int i = 1
        int j = l2 * 2;

        while (i <= l2) {
            if (i % 2 == 0) {
                if (j % 2 == 0) {
                    char t = arr[i - 1]
                    arr[i - 1] = arr[j - 1]
                    arr[j - 1] = t
                }
                j = j - 2
            }
            i++
        }
        newSent += new String(arr)+" "
    } else {
        newSent += word+" "
    }
}
println "output sentence >> "+newSent