How to resolve the algorithm Palindrome detection step by step in the Kotlin programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Palindrome detection step by step in the Kotlin programming language

Table of Contents

Problem Statement

A palindrome is a phrase which reads the same backward and forward. Write a function or program that checks whether a given sequence of characters (or, if you prefer, bytes) is a palindrome. For extra credit:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Palindrome detection step by step in the Kotlin programming language

1. Functions Declarations:

  • isExactPalindrome(s: String): Checks if the input string s is an exact palindrome, i.e., it reads the same forwards and backward.

  • isInexactPalindrome(s: String): Checks if the input string s is an inexact palindrome, i.e., it reads the same forwards and backward after ignoring case and non-alphanumeric characters.

2. Main Function:

  • main(args: Array) is the program's entry point.

3. Example Candidates:

  • candidates: An array of strings to check for exact palindromes.
  • candidates2: An array of strings to check for inexact palindromes.

4. Looping and Printing:

  • There are two loops to process the candidates arrays.
  • For each candidate, the program prints whether it is an exact or inexact palindrome.

5. Implementation Details:

  • isExactPalindrome uses the Kotlin String extension function reversed() to easily check if a string matches its reversed version.

  • isInexactPalindrome does the following:

    • Iterates over the original string s, filtering out non-alphanumeric characters and converting all characters to lowercase.
    • Stores the modified string in t.
    • Checks if t is equal to its reversed version.

6. Sample Output:

The output for the sample candidates will be:

'rotor' is an exact palindrome
'rosetta' is not an exact palindrome
'step on no pets' is an exact palindrome
'été' is an exact palindrome

'In girum imus nocte et consumimur igni' is an inexact palindrome
'Rise to vote, sir' is an inexact palindrome
'A man, a plan, a canal - Panama!' is an inexact palindrome
'Ce repère, Perec' is not an inexact palindrome

Source code in the kotlin programming language

// version 1.1.2

/* These functions deal automatically with Unicode as all strings are UTF-16 encoded in Kotlin */

fun isExactPalindrome(s: String) = (s == s.reversed())

fun isInexactPalindrome(s: String): Boolean {
    var t = ""
    for (c in s) if (c.isLetterOrDigit()) t += c
    t = t.toLowerCase()
    return t == t.reversed()
}

fun main(args: Array<String>) {
    val candidates = arrayOf("rotor", "rosetta", "step on no pets", "été")
    for (candidate in candidates) {
        println("'$candidate' is ${if (isExactPalindrome(candidate)) "an" else "not an"} exact palindrome")
    }
    println()
    val candidates2 = arrayOf(
         "In girum imus nocte et consumimur igni",
         "Rise to vote, sir",
         "A man, a plan, a canal - Panama!",
         "Ce repère, Perec"  // note: 'è' considered a distinct character from 'e'
    )
    for (candidate in candidates2) {
        println("'$candidate' is ${if (isInexactPalindrome(candidate)) "an" else "not an"} inexact palindrome")
    }
}

  

You may also check:How to resolve the algorithm Find largest left truncatable prime in a given base step by step in the Julia programming language
You may also check:How to resolve the algorithm Padovan sequence step by step in the Swift programming language
You may also check:How to resolve the algorithm Conditional structures step by step in the Brainf*** programming language
You may also check:How to resolve the algorithm String length step by step in the GW-BASIC programming language
You may also check:How to resolve the algorithm Run-length encoding step by step in the Icon and Unicon programming language