Lesson 1
Mastering String Manipulation with Scala: Reversing Words in Place
Introduction

Hello, and welcome! Are you ready to take your string manipulation skills to the next level? Today, we'll explore a task that enhances your understanding of strings and trains your ability to think creatively. The task at hand involves splitting a string into words and then reversing each word as if reflected in a mirror using Scala. Intrigued? Let's dive right in!

Task Statement and Description

Consider a string filled with words. Your task is to write a Scala function that accepts such a string. It should then take each of those words, reverse their character order, and finally stitch them all together to form a new string with reversed words.

Here's what you need to keep in mind:

  • The input string will contain between 1 and 100 words.
  • Each word is a sequence of characters separated by white space.
  • A word is composed of characters ranging from a to z, A to Z, 0 to 9, or even an underscore _.
  • The given string will not start or end with a space, and double spaces will not appear either.
  • After reversing the words, your program should return a single string with the words preserving their original order.

Example

Suppose the input string is "Hello neat scala_123".

The function will work on this in the following fashion:

  • 'Hello' becomes 'olleH'
  • 'neat' becomes 'taen'
  • 'scala_123' becomes '321_alacs'

The function combines the obtained strings into one string, resulting in "olleH taen 321_alacs".

Therefore, if solution("Hello neat scala_123") is called, the returned value should be "olleH taen 321_alacs".

Let's start breaking this down!

Step-by-Step Solution Building: Step 1

Our first step requires us to separate the words in the sentence. Scala provides us with a split method, which breaks a given string at specified separators and outputs an array of words. If you want to split by space, you can use split(" "):

Scala
1// an initial example string 2val inputStr = "Hello neat scala_123" 3 4// split the string into words 5val words = inputStr.split(" ") 6 7println(words.mkString(","))

You will see Hello,neat,scala_123 printed out, showing each word separated by a comma.

Step-by-Step Solution Building: Step 2

We've successfully extracted the words from the sentence, but they're not reversed yet. Scala provides a reverse method on strings to easily reverse their characters:

Scala
1// an example string 2val inputStr = "Hello neat scala_123" 3 4// split the string into words 5val words = inputStr.split(" ") 6 7// reverse each word 8val reversedWords = words.map(_.reverse) 9 10println(reversedWords.mkString(","))

Aha! Now you can see the reversed words: olleH,taen,321_alacs.

Step-by-Step Solution Building: Step 3

Finally, we need to bring these reversed words back together again into a single string with spaces as separators. We can use mkString(" ") to achieve this:

Scala
1def solution(inputStr: String): String = { 2 // split the string into words 3 val words = inputStr.split(" ") 4 5 // reverse each word 6 val reversedWords = words.map(_.reverse) 7 8 // join the words back together with space as a separator 9 val result = reversedWords.mkString(" ") 10 11 result 12} 13 14// Now we call the function and print the returned result outside of the function 15println(solution("Hello neat scala_123")) // this will print: 'olleH taen 321_alacs'
Lesson Summary

Well done! By completing this lesson, you've gained proficiency in manipulating strings using Scala, especially when it comes to reversing the order of characters in a word. I hope you're feeling more confident and enthusiastic about your Scala skills. Remember, the key to mastery is regular practice, so take a moment to explore related problems and practice what you’ve learned. It's all part of the joy of learning!

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