# The Swift algorithm implements an example of a method to literally flip a string

• 2020-05-24 06:17:20
• OfStack

preface

Flipping a string is common in string algorithms and is used by many companies as a pen test." "Flip the string verbatim" is a copy of the flip string, which is also the interview question of Google. The original question is:

``````
Given an input string, reverse the string word by word.
A word is defined as a sequence of non-space characters.
The input string does not contain leading or trailing spaces and the words are always separated by a single space.
For example,
Given s = "the sky is blue",
return "blue is sky the".
Could you do it in-place without allocating extra space?``````

In short: "the sky is blue" -- > "blue is sky the"

Therefore, for this paper, the algorithm to be solved is:

Flip the string verbatim, for example: "the sky is blue" -- > "blue is sky the"

Let's look at the implementation ideas and code.

Implementation ideas and code

Since it is a copy of the string inversion, we can use the previous version of the clone string to solve it, but this problem requires two flips:

First flip, whole flip: "the sky is blue" - > "eulb si yks eht"

Second flip, word flip: "eulb si yks eht" - > "blue is sky the"

So, first of all, we can implement an algorithm that can flip the local and all strings, passing in the character array, startIndex and endIndex, where startIndex and endIndex are respectively the initial subscript and the end subscript of the string to be flipped, that is, the characters (including) between startIndex and endIndex are flipped, the code is as follows:

``````
func _reverseStr( _ chars:inout [Character], _ startIndex:Int, _ endIndex:Int){

var startIndex = startIndex
var endIndex = endIndex

if startIndex <= endIndex {

let tempChar = chars[endIndex]
chars[endIndex] = chars[startIndex]
chars[startIndex] = tempChar

startIndex += 1
endIndex -= 1

_reverseStr(&chars,startIndex,endIndex)

}

}``````

After that, the algorithm above can be used to complete the two flips mentioned above:

``````
func reverseWords(_ str:String) -> String{

var chars = [Character](str.characters)

// First, flip the entire string of characters ,"the sky is blue" -> "eulb si yks eht"
_reverseStr(&chars,0,chars.count-1)

// Then flip the characters in each word, "eulb si yks eht" -> "blue is sky the"
var startIndex = 0
for endIndex in 0 ..< chars.count {
if endIndex == chars.count - 1 || chars[endIndex + 1] == " " {
_reverseStr(&chars, startIndex, endIndex)
startIndex = endIndex + 2
}
}

return String(chars)
}``````

Complete algorithm code:

``````
// Flips the specified range of characters
func _reverseStr( _ chars:inout [Character], _ startIndex:Int, _ endIndex:Int){

var startIndex = startIndex
var endIndex = endIndex

if startIndex <= endIndex {

let tempChar = chars[endIndex]
chars[endIndex] = chars[startIndex]
chars[startIndex] = tempChar

startIndex += 1
endIndex -= 1

_reverseStr(&chars,startIndex,endIndex)

}

}

// Flipping a string verbatim
func reverseWords(_ str:String) -> String{

var chars = [Character](str.characters)

// First, flip the entire string of characters ,"the sky is blue" -> "eulb si yks eht"
_reverseStr(&chars,0,chars.count-1)

// Then flip the characters in each word, "eulb si yks eht" -> "blue is sky the"
var startIndex = 0
for endIndex in 0 ..< chars.count {
if endIndex == chars.count - 1 || chars[endIndex + 1] == " " {
_reverseStr(&chars, startIndex, endIndex)
startIndex = endIndex + 2
}
}

return String(chars)
}

reverseWords("the sky is blue") //return "blue is sky the"``````

conclusion

Related articles: