3

So I have a textField where you should enter your "coded" text and get it translated back to non-coded language by using .replace to remove certain characters. But I can't get it to work.

There is a kids "code-language" where you take a word, like cat and for every consonant you add an "o" and the consonant again. So a "b" would be "bob". With vowels they just stay as they are. Cat would be cocatot.

fun translateBack(view : View) {
     val konsonanter = "bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXZ"
     var input = editText.text.toString()
     var emptyString = ""

     for(i in konsonanter) {
        val find_text = i + "o" + i

        var conso = i.toString()

        textView.text = input.replace(find_text, conso, false)
     }
 }

Would like for it to remove the two following letters for every consonant (if possible). So if i enter "cocowow" I should get out "cow". Right now I just get back what I enter in the textField...

3 Answers 3

2

Use a forEach loop through the chars of input and replace:

konsonanter.forEach { input = input.replace("${it}o${it}", "${it}", false) }
textView.text = input
Sign up to request clarification or add additional context in comments.

6 Comments

I don't know if OP needs it, but this would not work for "Bob" (uppercase B at the start). I addressed that in my answer.
@WilliMentzel if input is "Bob" then the result is "Bob" also because of false for the ignoreCase argument. Why do you say that this would not work for "Bob"?
shouldn't "Bob" be turned to "b"? maybe I misunderstood it
No, "bob" should be turned to "b" and "BoB" should be turned to "B".
maybe you know this game better than I do, I don't think it's clear from the question... who knows. good answer anyways.
|
1

The issue is that you're setting the text in textView in every loop, and never updating input. So you're essentially only seeing the result of the replace call that happens with the "ZoZ" and "Z" parameters at the end of the loop, with input still being the original string.

Instead, you can keep updating input and then set the text when you're done:

val konsonanter = "bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXZ"
var input = editText.text.toString()
var emptyString = ""

for (i in konsonanter) {
    val find_text = i + "o" + i

    val conso = i.toString()

    input = input.replace(find_text, conso, false)
}

textView.text = input

Comments

1

If you use the replace function with a Regex and a transform function as parameters you can create a really concise completely self-containing extension function:

fun String.translateBack() = with(Regex("([bcdfghjklmnpqrstvwxz])o\\1", RegexOption.IGNORE_CASE)) {
    replace(this) { "${it.value.first()}" }
}

Explanation:

The Regex will match all same consonants (no matter the case) around an "o". To ensure that the consonant before and after the "o" are the same a backreference to the first group was used.

So, this will also work for cases like "coCatot".

Usage:

println("bob".translateBack()) // b
println("cocatot".translateBack()) // cat
println("coCatot".translateBack()) // cat

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.