不使用预定义函数反转字符串

g6ll5ycj  于 2022-09-19  发布在  Swift
关注(0)|答案(7)|浏览(236)

原谅我,因为我是这门语言的新手。

编辑:有没有办法反转数组元素的位置?

我正在尝试创建一个函数来测试给定的输入是否为回文。我正在尝试避免使用Reverted()函数

let word = ["T","E","S","T"]
var temp = [String]()
let index_count = 3

for words in word{
    var text:String = words
    print(text)
    temp.insert(text, atIndex:index_count)
    index_count = index_count - 1
}
slmsl1lt

slmsl1lt1#

您的方法可以用于反转数组。但您必须在目标数组的开始位置插入原始数组的每个元素(将其他元素移动到末尾):

// Swift 2.2:
let word = ["T", "E", "S", "T"]
var reversed = [String]()
for char in word {
    reversed.insert(char, atIndex: 0)
}
print(reversed) // ["T", "S", "E", "T"]

// Swift 3:
let word = ["T", "E", "S", "T"]
var reversed = [String]()
for char in word {
    reversed.insert(char, at: 0)
}
print(reversed) // ["T", "S", "E", "T"]

可以直接对字符串的字符执行相同的操作:

// Swift 2.2:
let word = "TEST"
var reversed = ""
for char in word.characters {
    reversed.insert(char, atIndex: reversed.startIndex)
}
print(reversed) // "TSET"

// Swift 3:
let word = "TEST"
var reversed = ""
for char in word.characters {
    reversed.insert(char, at: reversed.startIndex)
}
print(reversed)
t3psigkw

t3psigkw2#

SWIFT 5

extension String {
    func invertedEntropy() -> String {
        var word = [Character]()
        for char in self {
            word.insert(char, at: 0)
        }
        return String(word)
    }
}
var palindrome = "TENET"
palindrome.invertedEntropy()

// "TENET"
a11xaf1n

a11xaf1n3#

以下是我的解决方案:

extension String {
    func customReverse() -> String {
        var chars = Array(self)
        let count = chars.count

        for i in 0 ..< count/2 {
            chars.swapAt(i, count - 1 - i)
        }

        return String(chars)
    }
}

let input = "abcdef"
let output = input.customReverse()
print(output)

你可以在这里试一试。

6jjcrrmo

6jjcrrmo4#

func reverse(_ str: String) -> String {

    let arr = Array(str) // turn the string into an array of all of the letters

    let reversed = ""

    for char in arr {

        reversed.insert(char, at: reversed.startIndex)
    }

    return reversed
}

要使用它,请执行以下操作:

let word = "hola"

let wordReversed = reverse(word)

print(wordReversed) // prints aloh
igetnqfo

igetnqfo5#

另一种逆转的解决方案:

var original : String = "Test"
var reversed : String = ""
var c = original.characters
for _ in 0..<c.count{
    reversed.append(c.popLast()!)
}

它只是附加弹出的旧字符串的每个元素,从最后一个元素开始,一直到第一个元素

9fkzdhlc

9fkzdhlc6#

方案一

let word = "aabbaa"

let chars = word.characters
let half = chars.count / 2
let leftSide = Array(chars)[0..<half]
let rightSide = Array(chars.reverse())[0..<half]
let palindrome = leftSide == rightSide

方案二

var palindrome = true
let chars = Array(word.characters)
for i in 0 ..< (chars.count / 2) {
    if chars[i] != chars[chars.count - 1 - i] {
        palindrome = false
        break
    }
}
print(palindrome)
uqjltbpv

uqjltbpv7#

static func reverseString(str : String) {
        var data = Array(str)
        var i = 0// initial
        var j = data.count // final
        //either j or i for while , data.count/2 buz only half we need check
        while j != data.count/2  {
            print("befor i:(i) j:(j)" )
            j = j-1 
            data.swapAt(i, j) //  swapAt API avalible only for array in swift
            i = i+1 
        }
        print(String(data))
    }

相关问题