颠倒字符串值的顺序

时间:2017-01-21 16:49:46

标签: swift function character

我有以下功能可以反转字符串值的显示顺序。 我是Swift的新手,我试图理解它的逻辑。 '!pleh'值的变化是什么变成了'帮助!' ? 感谢

func reverse(_ s: String) -> String {
 var str = ""
 for character in s.characters {
    str = "\(character)" + str
 }
 return str
}
print (reverse("!pleH"))

14 个答案:

答案 0 :(得分:11)

在swift 4.0中,直接在字符串上调用reversed将完成工作

let str = "abc"
String(str.reversed()) // This will give you cba

答案 1 :(得分:6)

函数reversed(_:)遍历提供的字符串Character中的每个s,并以相反的顺序将它们集中在一起。

// 1st pass in the 'for' loop:
//    @ start of iteration: 'str' equals ""
//    update value of 'str' according to
//    str = aCharacterAsString + str = "!" + ""

// 2nd pass in the 'for' loop:
//    @ start of iteration: str equals "!"
//    update value of 'str' according to
//    str = anotherCharacterAsString + str = "p" + "!"

// 3rd pass in the 'for' loop:
//    @ start of iteration: str equals "p!"
//    update value of 'str' according to
//    str = yetAnotherCharacterAsString + str = "l" + "p!"

// and so on ...

// after end of 'for' loop: str == "help!"

// this value of 'str' is then return to callee (which prints it)

更简单的方法是在reversed()实例的CharacterView上使用String

let str = "!pleH"
print(String(str.characters.reversed())) // Help!

Swift 4 您可以直接在字符串

中使用它
let str = "!pleH"
print(String(str.reversed())) // Help!

答案 2 :(得分:6)

内联评论,

func reverse(_ s: String) -> String {
 var str = ""
 //.characters gives the character view of the string passed. You can think of it as array of characters.
 for character in s.characters {
    str = "\(character)" + str
    //This will help you understand the logic. 
    //!+""
    //p+!
    //l+p! ... goes this way
    print ( str)
 }
 return str
}
print (reverse("!pleH"))

答案 3 :(得分:4)

var string = "My,playground and my Swift" 
var reverseString = ""  
for str in string { 
    reverseString.insert(str, at: reverseString.startIndex) 
}  
print(reverseString)

答案 4 :(得分:2)

注意:如果您不想使用reversed()内置函数,则可以使用以下代码。[要求进行采访]

在字符串上使用高阶函数“ Reduce ”的单行代码。

extension String {
func reverse() -> String  { return self.reduce("") { "\($1)" + $0 } }
}

答案 5 :(得分:0)

在Swift 4中 - 反向字符串::

func reverse(string:String) -> String {

    var reverse = ""

    for char  in string {

        reverse = char.description + reverse
    }
    return reverse
}
  

输入 :: reverse(字符串:" Ashish Chhabra")

     

输出 :: arbahhC hsihsA

答案 6 :(得分:0)

使用字符和索引的答案有不同的变化。

 //Reverse String
func reverse(str:String)->String{
    var chars = [Character]()
    for i in (0...str.count).reversed() {
       let index = str.index(str.startIndex, offsetBy: i)
       chars.append(str[index])
    }
    return String(chars)
}

答案 7 :(得分:0)

您可以使用以下代码-

let str = "My name is Kplin Cours"
 var reverseString = ""
 for i in 0..<str.count {
  let index = str.index(str.startIndex, offsetBy: (str.count - 1) - i)
//  print(String(str[index]))
 reverseString = reverseString + String(str[index])
}

print(reverseString)  //sruoC nilpK si eman yM

答案 8 :(得分:0)

let string = readLine()!
var resultString = ""
for i in 1...string.count {
    let index = string.index(string.endIndex, offsetBy: -i)
    resultString.append(string[index])
}
print(resultString)

答案 9 :(得分:0)

var strnew = "hello world new" 

var reverseStr = ""

for each in strnew
{
    reverseStr = String(each) + reverseStr
}

print(reverseStr)

答案 10 :(得分:0)

let inputstr = "ABCDEFGHIGKLMNPO"
var resultstr = "";
    for strchar in inputstr {
     resultstr = String(strchar) + resultstr
    }
                  print("Result = ",resultstr)

答案 11 :(得分:0)

斯威夫特 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"

答案 12 :(得分:0)

就这么简单!

let str = "Hello, world!"
let reversed = String(str.reversed())
print(reversed)

答案 13 :(得分:-1)

var str="abc"
//this will print the reverse of the string
print(String(str.characters.reversed()))