Avatar of motenrique

motenrique's solution

to Reverse String in the Kotlin Track

Published at Oct 18 2019 · 0 comments
Instructions
Test suite
Solution

Reverse a string

For example: input: "cool" output: "looc"

Setup

Go through the setup instructions for Kotlin to install the necessary dependencies:

https://exercism.io/tracks/kotlin/installation

Making the test suite pass

Execute the tests with:

$ gradlew test

Use gradlew.bat if you're on Windows

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by removing the @Ignore annotation.

Source

Introductory challenge to reverse an input string https://medium.freecodecamp.org/how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

ReverseStringTest.kt

import org.junit.Ignore
import org.junit.Test
import kotlin.test.assertEquals

class ReverseStringTest {
    @Test
    fun testAnEmptyString() {
        assertEquals("", reverse(""))
    }

    @Ignore
    @Test
    fun testAWord() {
        assertEquals("tobor", reverse("robot"))
    }

    @Ignore
    @Test
    fun testACapitalizedWord() {
        assertEquals("nemaR", reverse("Ramen"))
    }

    @Ignore
    @Test
    fun testASentenceWithPunctuation() {
        assertEquals("!yrgnuh m'I", reverse("I'm hungry!"))
    }

    @Ignore
    @Test
    fun testAPalindrome() {
        assertEquals("racecar", reverse("racecar"))
    }

}
fun reverse(word: String) :String {

    var reversedString = ""

    for (idx in word.length-1 downTo 0) {
        reversedString = reversedString.plus( word[idx] )
    }

    return reversedString
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?