Ad space (banner)
๐ŸฆSwift Lessons
Lesson 43 / 68

Reversing a String and Checking for Palindromes

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

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

The sample code reverses the string with String(str.reversed()) inside isPalindrome() and compares it with the original str. "racecar" comes out the same either way, so it is reported as a palindrome.

A common stumbling block for beginners is how this differs from string reversal in other languages. Swift strings work in units of grapheme clusters, so even emoji are handled safely - a safer reversal than in languages that work byte by byte.

It is one of the simple, readable idioms you get from Swift treating a string as a sequence of characters, and palindrome checking is a classic learning exercise.

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

๐Ÿงช This site can't compile or run Swift 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)