kotlin 字符串反转
Given a string, we have to reverse its each word.
给定一个字符串,我们必须反转其每个单词。
Example:
例:
Input:
string = "Hello world"
Output:
"olleH dlrow"
程序在Kotlin中反转每个单词 (Program to reverse each word in Kotlin)
At first, we are reading a string, then splitting and converting the string to a string list, then extracting each word, reversing the words, and finally creating the string with reverse words.
首先,我们正在读取一个字符串,然后将其拆分并转换为字符串列表,然后提取每个单词,将单词反转,最后创建带有反向单词的字符串。
package com.includehelp.basic
import java.util.*
//Method to reverse each word in provided April20.string
fun reverseWord(inputString: String): String {
//split April20.string by space
val strList = inputString.split(" ") // Spilt String by Space
val sb = StringBuilder()
//iterate April20.string List
for (items in strList) {
if (items != "") {
//reverse List item and reverse them
val rev = StringBuilder(items).reverse().toString()
//append reverse April20.string into String Builder
sb.append("$rev ")
}
}
//return final reverse April20.string
return sb.toString()
}
//Main Function, entry Point of Program
fun main(args: Array<String>) {
//Input Stream
val sc = Scanner(System.`in`)
//input April20.string value
println("Input String : ")
val str: String = sc.nextLine()
println("Input String : $str")
//Call function for reverse words in April20.string and print them
println("String with Reverse Word : " + reverseWord(str))
}
Output
输出量
Run 1:
Input String :
Hello include help
Input String : Hello include help
String with Reverse Word : olleH edulcni pleh
---
Input String :
Hello I am in Delhi, How are you
Input String : Hello I am in Delhi, How are you
String with Reverse Word : olleH I ma ni ,ihleD woH era uoy
翻译自: https://www.includehelp.com/kotlin/reverse-each-word-in-string.aspx
kotlin 字符串反转