Reverse Words in a String using kotlin

Given an input string, reverse the string word by word and reverse the letters in the words. A word is defined as a sequence of non-space characters.

The input string does not contain leading or trailing spaces and the words are always separated by a single space.


For example,
Given string = "The quick brown fox jumps over the lazy dog",
return "ehT kciuq nworb xof spmuj revo eht yzal god ".


Could you do it in-place without allocating extra space/Array?

Kotlin program


fun reverseSentance(str: String):String {
    /* The split() method of String class splits
	 * a string in several strings based on the
	 * delimiter passed as an argument to it
	 */
    val words = str.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
    var reversedString = ""
    for (i in words.indices) {
        val word = words[i]
        var reverseWord = ""
        for (j in word.length - 1 downTo 0) {
            /* The charAt() function returns the character
             * at the given position in a string
             */
            reverseWord = reverseWord + word[j]
        }
        reversedString = "$reversedString$reverseWord "
    }
    return reversedString
}

fun main(args: Array<String>) {
    var str = "The quick brown fox jumps over the lazy dog"
    println("Original string : " +str)
    println("Reversed String : "+reverseSentance(str))
}


Output


Original string : The quick brown fox jumps over the lazy dog
Reversed String : ehT kciuq nworb xof spmuj revo eht yzal god 

Reverse words of a string in reverse order (without reversing letters in the word) with kotlin


Example


Input: "the sky is blue",
Output: "blue is sky the".


fun wordReverse(str: String): String {
    var i = str.length - 1
    var start: Int
    var end = i + 1
    var result = ""

    while (i >= 0) {
        if (str[i] == ' ') {
            start = i + 1
            while (start != end)
                result += str[start++]

            result += ' '.toString()

            end = i
        }
        i--
    }

    start = 0
    while (start != end)
        result += str[start++]

    return result
}

fun main(args: Array<String>) {
    val str = "the sky is blue"
    println(str)
    print(wordReverse(str))
}


Output of the kotlin program


the sky is blue
blue is sky the

Reverse a sentance using stack in kotlin



Kotlin program to reverse a sentance


fun main(args: Array<String>) {
    val str = "the sky is blue"
    println(str)

    var stk = Stack()
    for (words in str.split(" ")){
        stk.push(words)
    }

    var reverseString = ""

    for (j in 0..stk.size-1){
        reverseString +=stk.pop()

        if(j != stk.size){
            reverseString += " "
        }
    }
    print(reverseString)
}


Output


the sky is blue
blue is sky the

 
Join My Facebook Group
Join Group
 

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions