Lesson 1
String Manipulation: Reversing Words in Kotlin
Introduction

Hello, and welcome! Are you ready to elevate your string manipulation skills? Today, we'll delve into a task that will bolster your comprehension of strings and enhance your creativity. The task involves splitting a string into words and then reversing each word as if reflected in a mirror. Does that sound interesting? Let's get started!

Task Statement and Description

You're tasked with considering a string filled with words and writing a function that accepts this string. The function should reverse the character order of each word and form a new string consisting of these reversed words.

Here's what you need to keep in mind:

  • The input string will contain between 1 and 100 words.
  • Each word in the string is a sequence of characters separated by whitespace.
  • The characters can range from a to z, A to Z, 0 to 9, or even an underscore _.
  • The provided string will neither start nor end with space, and double spaces won't be present either.
  • After reversing the words, your program should output a single string with the reversed words preserving their original order.

Example

Consider the input string "Hello neat java_lovers_123".

The function works as follows:

  • Hello becomes olleH
  • neat becomes taen
  • java_lovers_123 becomes 321_srevol_avaj

Afterward, it forms a single string with these reversed words, producing "olleH taen 321_srevol_avaj".

Therefore, if you call reverseWords("Hello neat java_lovers_123"), the function should return "olleH taen 321_srevol_avaj".

Let's begin breaking this down!

Step-by-Step Solution Building: Step 1

Our first task is to separate the words in the sentence. In Kotlin, you can use the split() method to achieve this easily. The delimiter you'll use in the split() method is a single space " ". Here is a sample code to illustrate this:

Kotlin
1val inputStr = "Hello neat java_lovers_123" 2val words = inputStr.split(" ") 3 4// Now the list 'words' holds all the words of the string

Note that " " as the delimiter ensures that the string is split at each space, effectively separating the words.

Step-by-Step Solution Building: Step 2

Next, we need to reverse each word separated in the previous step. In Kotlin, we can use the reversed() method available on strings to achieve this. Let's add these lines to our existing code:

Kotlin
1val reversedWords = words.map { it.reversed() } 2 3// 'reversedWords' now contains the reversed words
Step-by-Step Solution Building: Step 3

Finally, we need to consolidate these reversed words into a single string, separated by spaces. This can be conveniently achieved using the joinToString() function in Kotlin. Here's how we do that:

Kotlin
1val result = reversedWords.joinToString(" ")

First, we join all the reversed words while preserving the format. The resulting string can be directly obtained using joinToString().

Final Solution

It remains for us to combine the code from the steps together in a function reverseWords and call it from the main function to test.

Kotlin
1fun reverseWords(inputStr: String): String { 2 val words = inputStr.split(" ") 3 val reversedWords = words.map { it.reversed() }.toTypedArray() 4 return reversedWords.joinToString(" ") 5} 6 7fun main() { 8 // Call the function 9 println(reverseWords("Hello neat java_lovers_123")) // prints: 'olleH taen 321_srevol_avaj' 10}
Lesson Summary

Well done! By completing this lesson, you've sharpened your proficiency in manipulating strings using Kotlin. You've improved, especially in reversing the order of characters in a word. I hope you're feeling confident and excited about your Kotlin skills. Remember, mastering these skills requires frequent practice. Therefore, take some time to explore related problems and practice what you’ve learned. Enjoy the journey of learning!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.