博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
kotlin 字符串反转_Kotlin程序反转字符串中的每个单词
阅读量:2529 次
发布时间:2019-05-11

本文共 1811 字,大约阅读时间需要 6 分钟。

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.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/

你可能感兴趣的文章
REDHAT系统创建LVM过程
查看>>
PLC梯形图编程练习
查看>>
算法总结1:K-邻近算法
查看>>
unity, 由Matrix4x4提取Quaternion和Vector3 及 由Quaternion,Vector3构造Matrix4x4
查看>>
6服务器线程池配置
查看>>
thinkPHP实现静态页的方法-buildHtml
查看>>
Openwrt 远程调试
查看>>
dubbo结果缓存机制
查看>>
有些验证码看起来很容易但是没人做自动识别的原因分析
查看>>
用Delphi中的TADOQuery查询Oracle10G中的数据表时,Open时提示"数据类型不被支持"的处理方法...
查看>>
基本知识点
查看>>
socket
查看>>
python中list、tuple、dict、set的使用
查看>>
【c++ primer读书笔记】【第8章】IO流
查看>>
UWP App Services in Windows 10
查看>>
spring boot 定时任务
查看>>
在onclick事件中传递对象参数
查看>>
2017.11.20
查看>>
HTTP服务器
查看>>
SilverLight:”无法启动调试--未安装 Silverlight Developer 运行时。请安装一个匹配版本”的解决方案。...
查看>>