Reverse a String in Swift

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


Comments

5 responses to “Reverse a String in Swift”

  1. or short:

    var str = “Hello, playground”

    var reverseStr = “”

    for character in str {
    reverseStr = character + reverseStr
    }

    1. Thanks for the suggestion.

  2. for char in identityNumber{
    reversedString = char + reversedString

    }
    ‘String’ is not convertible to ‘Character’

    1. 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
      }

  3. Even simpler, using the builtin collections functions:

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

Leave a 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.