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

news/2024/7/19 12:38:55 标签: 字符串, 列表, python, 正则表达式, .net

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


http://www.niftyadmin.cn/n/1254203.html

相关文章

mysql上移字段语句_mysql让自增字段重新排序(上移、下移、置顶时遇到问题解决方案)...

1、背景说明&#xff1a;在做商城项目分类的时候&#xff0c;我们经常会遇到关于分类上移、下移、置顶和置底操作&#xff0c;至于上移和下移操作相对来说比较简单&#xff0c;上移将当前数据排序字段的值和上一调数据的排序字段值进行调换&#xff0c;下移也是同理&#xff0c…

scala部分应用函数_Scala中的部分应用函数

scala部分应用函数部分应用的功能 (Partially applied functions) Partially applied functions, are actually those function that allows you to implement function calls with partial arguments i.e. using only a few values in a function call and uses rest values f…

基于ai的_基于人工智能的代理

基于ai的An artificial intelligent based agent is responsible for any work output obtained from the system. It is the main component which while dealing with artificial intelligence because it is the intelligent system which is needed to be developed. An ag…

aptitude_PHP JSON Aptitude问题与解答

aptitudeThis section contains Aptitude Questions and Answers on PHP JSON. 本节包含有关PHP JSON的 Aptitude问题和解答。 1) There are the following statements that are given below, which of them are correct about JSON in PHP? JSON stands for JavaScript Obje…

ruby字符串截取字符串_Ruby程序反向字符串 套装2

ruby字符串截取字符串Ruby| 倒弦 (Ruby | Reversing string) Here, we are implementing a Ruby program to reverse a string. 在这里&#xff0c;我们正在实现一个Ruby程序来反转字符串。 Methods used: 使用的方法&#xff1a; gets: This method is a public instance me…

java treemap_Java TreeMap clear()方法与示例

java treemapTreeMap类clear()方法 (TreeMap Class clear() method) clear() method is available in java.util package. clear()方法在java.util包中可用。 clear() method is used to clear all of the existing mappings from this TreeMap. clear()方法用于清除此TreeMap中…

java enumset_Java EnumSet clone()方法与示例

java enumsetEnumSet类clone()方法 (EnumSet Class clone() method) clone() method is available in java.util package. clone()方法在java.util包中可用。 clone() method is used to return a shallow copy of this EnumSet. clone()方法用于返回此EnumSet的浅表副本。 clo…

navicat连接MySQL时出现No_使用Navicat连接数据库时出现了这个问题?解决办法来了!...

使用Navicat for Oracle连接远程Oracle数据库服务器时&#xff0c;程序报错ORA-28547:(如下图所示)错误截图错误原因:Navicat for Oracle程序的oci文件和远程或本地数据库的oci版本不一致。原因分析&#xff1a;Navicat for oracle是通过Oracle客户端连接Oracle服务器的&#x…