Joseph Jude

Consult . Code . Coach

Swift Challenge 002: Check if a string is palindrome


code . swift

Challenge: Write a program to check if the string entered by the user is a palindrome. That is that it reads the same forwards as backwards like "racecar"

Logic: Reverse the string and compare. If equal, then it is palindrome

Code:

print("Enter a string: ")
let userInput = readLine(stripNewline: true)!

let reverse = String(userInput.characters.reverse())

if (reverse == userInput) {
  print("\(userInput) is a palindrome")
} else {
  print("\(userInput) is not a palindrome")
}

Explanation:

To Improve:

Uses:

Git Repository / All Swift Challenges

Got comments? Tweet it, or comment below.


Like the post? Retweet it. Got comments? Reply.

Swift Challenge 002: Check if a string is palindrome by @jjude: https://t.co/dB7kAxBbQ2

— Joseph Jude (@jjude) October 6, 2016
Share this on: Twitter / /

Comments

comments powered by Disqus