本文共 1811 字,大约阅读时间需要 6 分钟。
kotlin 字符串反转
Given a string, we have to reverse its each word.
给定一个字符串,我们必须反转其每个单词。
Example:
例:
Input: string = "Hello world" Output: "olleH dlrow"
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.basicimport java.util.*//Method to reverse each word in provided April20.stringfun 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 Programfun main(args: Array) { //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 helpInput String : Hello include helpString with Reverse Word : olleH edulcni pleh---Input String :Hello I am in Delhi, How are youInput String : Hello I am in Delhi, How are youString with Reverse Word : olleH I ma ni ,ihleD woH era uoy
翻译自:
kotlin 字符串反转
转载地址:http://fwozd.baihongyu.com/