1. Introduction

A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward. In other words, it is a string that remains unchanged when its characters are reversed. For example, “racecar” is a palindrome because when we reverse the letters, it still spells “racecar”.

In this tutorial, we will explore various ways to check if a string is a palindrome using Kotlin.

2. Strings to Be Tested

Now, let’s take a look at various ways we can check if a string is palindromic using code in Kotlin. The palindromes we will test are:

  • racecar
  • redivider
  • 3553
  • tattarrattat

The words that are not palindromes we will test are:

  • palindrome
  • sever

3. Using a Loop

One way to check if a string is a palindrome is to loop through it and compare the characters at the beginning and end of the string. If they are the same, we move on to the next pair of characters until we reach the middle of the string:

fun isPalindrome(str: String): Boolean {
    var start = 0
    var end = str.length - 1
    while (start < end) {
        if (str[start] != str[end]) {
            return false
        }
        start++
        end--
    }
    return true
}

Let’s now test our function:

@Test
fun `palindrome check using loop function`(){
    assertTrue(isPalindromeUsingLoop("racecar"))
    assertTrue(isPalindromeUsingLoop("redivider"))
    assertFalse(isPalindromeUsingLoop("palindrome"))
    assertFalse(isPalindromeUsingLoop("sever"))
    assertTrue(isPalindromeUsingLoop("3553"))
    assertTrue(isPalindromeUsingLoop("tattarrattat"))
}

We notice that words like “palindrome” and “sever” don’t qualify as palindromes. This is because these words have a different arrangement of characters when they are reversed. Their reversed forms will read “emordnilap” and “reves” respectively. However, strings such as “redivider” and “3553” stay unchanged when reversed.

4. Using reversed()

Another approach involves using Kotlin’s built-in function called reversed(). Basically, this function helps us to reverse a string, and we can use it to check if a string is a palindrome by comparing the reversed string to the original string:

fun isPalindromeWithBuiltInFunction(str: String): Boolean {
    val reversedStr = str.reversed()
    return str == reversedStr
}

Let’s test our new method:

@Test 
fun `palindrome check using built in function`(){ 
    assertTrue(isPalindromeWithBuiltInFunction("racecar"))
    assertTrue(isPalindromeWithBuiltInFunction("redivider"))
    assertFalse(isPalindromeWithBuiltInFunction("palindrome"))
    assertFalse(isPalindromeWithBuiltInFunction("sever"))
    assertTrue(isPalindromeWithBuiltInFunction("3553"))
    assertTrue(isPalindromeWithBuiltInFunction("tattarrattat")) 
}

5. Using Recursion

The last method we’re going to look at is the recursive approach. In this method, we check the first and last characters from our string and repeat the process with the remaining portion of the string until we reach the middle of the string:

fun isPalindromeUsingRecursion(str: String): Boolean {
    if (str.length <= 1) {
        return true
    }
    if (str.first() != str.last()) {
        return false
    }
    return isPalindromeUsingRecursion(str.substring(1, str.length - 1))
}

Of course, we need some test cases for this function:

@Test fun `palindrome check using recursion`(){
    assertTrue(isPalindromeUsingRecursion("racecar"))
    assertTrue(isPalindromeUsingRecursion("redivider"))
    assertFalse(isPalindromeUsingRecursion("palindrome"))
    assertFalse(isPalindromeUsingRecursion("sever"))
    assertTrue(isPalindromeUsingRecursion("3553"))
    assertTrue(isPalindromeUsingRecursion("tattarrattat"))
}

6. Conclusion

To sum up, we’ve explored various methods we can employ to determine if a given string is a palindrome in Kotlin. We used loops, built-in functions, and recursion to do so. Also, we demonstrated the use of each method with appropriate test cases.

As always, the code samples and relevant test cases pertaining to this article can be found over on GitHub.


« 上一篇: KDoc介绍
» 下一篇: SQLDelight指南