• Skip to main content
  • Skip to primary sidebar

Ravi Shankar

Tweaking Apps

  • Swift
  • Tech Tips
You are here: Home / ios / Reverse a String in Swift

Reverse a String in Swift

July 2, 2014 By Ravi Shankar 5 Comments

Here is a simple code snippet written in Swift programming language for reversing a string.

import Cocoa


//Assigning a value to a String variable

var str = "Hello, playground"


//Create empty character Array.

var strArray:Character[] = Character[]()


//Loop through each character in the String

for character in str {

//Insert the character in the Array variable.

strArray.append(character)

}


//Create a empty string

var reversedStr:String = ""


//Read the array from backwards to get the characters

for var index = strArray.count - 1; index >= 0;--index {

//Concatenate character to String.

reversedStr += strArray[index]

}


reversedStr


the shorter version to reverse is (thanks Andreas)


var str = “Hello, playground”

var reverseStr = “”

for character in str {

reverseStr = character + reverseStr

}

Reverse a String in Swift Programming language
This code snippet demonstrates the following.

  • How to assign a value to variable.
  • How to create an Array of Characters and assign empty value.(Character)
  • Iterate over the string using for-in loop.
  • How to add new elements to an Array.
  • How to create empty String variable.
  • Use the standard for loop to traverse through an array.
  • Concatenate Strings and character
  • Using for .. in

Filed Under: ios, Programming, Xcode Tagged With: Reverse String, Swift, Xcode

Reader Interactions

Comments

  1. andreas says

    August 6, 2014 at 6:37 pm

    or short:

    var str = “Hello, playground”

    var reverseStr = “”

    for character in str {
    reverseStr = character + reverseStr
    }

    Reply
    • rshankar says

      August 7, 2014 at 1:48 am

      Thanks for the suggestion.

      Reply
  2. Pedro says

    August 26, 2014 at 2:57 am

    for char in identityNumber{
    reversedString = char + reversedString

    }
    ‘String’ is not convertible to ‘Character’

    Reply
    • rshankar says

      August 26, 2014 at 3:20 am

      Looks like some changes have been introduced in Xcode 6 beta 6. Here is the fix.

      var str = “Hello, playground”

      var reverseStr = “”

      for character in str {
      reverseStr = String(character) + reverseStr
      }

      Reply
  3. doches says

    October 31, 2014 at 8:14 pm

    Even simpler, using the builtin collections functions:

    var str = “abcdef”
    var revStr = String(reverse(str))

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

TwitterLinkedin

Recent Posts

  • How to block keywords in Jio broadband
  • How to disable opening an app automatically at login in Mac
  • How to set preferred Wifi network on Mac
  • Attribute Unavailable: Estimated section warning before iOS 11.0
  • How to recover Firefox password from Time Machine backup

Pages

  • About
  • Privacy Policy
  • Terms and Conditions

Copyright 2022 © rshankar.com

Terms and Conditions - Privacy Policy