Ad space (banner)
๐ŸŽฏKotlin Lessons
Lesson 43 / 68

Reversing a String and Checking for Palindromes

In this lesson you will learn how to reverse a string in Kotlin and check whether it is a palindrome, covering the basics of string manipulation. This is for anyone searching for "Kotlin reverse string palindrome check".

.reversed() reverses a string. A string that reads the same forwards and backwards is called a palindrome.

The sample code writes isPalindrome() as an expression-bodied function (=) comparing the original with its reverse in the single line str == str.reversed(). "racecar" comes out the same either way, so it is reported as a palindrome.

A common stumbling block for beginners is the expression-bodied form itself. Defining a function with just = expression, with no braces and no return, is concise, idiomatic Kotlin that takes a moment to get used to.

You will rarely reverse a string in production, but as practice at working with text it is a classic learning exercise.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Kotlin directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)