kotlin 字符串反转_Kotlin程序反转字符串中的每个单词

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 字符串反转

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值