How to reverse a String in Scala?
Last Updated :
29 Apr, 2024
In this article, we will learn to reverse a string in Scala. Reverse of a string means changing its order so that the last character becomes the first, the second-to-last becomes the second, and so on, effectively flipping the string's sequence of characters.
Examples:
Input: S = "GeeksforGeeks"
Output: skeeGrofskeeG
Input: S= "String"
Output: gnirtS
Reverse a String in Scala
Below are the possible approaches to reverse a string in Scala.
Approach 1: Using the reverse method
- In this approach, we are using the reverse method available for strings in Scala, which directly reverses the order of characters in the string.
- This method provides a simple and concise way to reverse a string without the need for manual iteration or recursion.
- By calling reverse on the string, we obtain the reversed version of the original string.
In the below example, the reverse method is used to reverse a string in Scala.
Scala
object GFG {
def main(args: Array[String]): Unit = {
val str = "GeeksforGeeks"
val reversedStr = str.reverse
println(reversedStr)
}
}
Output:

Time Complexity: O(N)
Auxiliary Space: O(1)
Approach 2: Using a Loop
- In this approach, we initialize an empty string reversedStr and iterate over the characters of the input string str in reverse order using a for loop.
- Within each iteration, we append the character at index i to the reversedStr string, effectively reversing the original string.
- Finally, we print the reversed string using println(reversedStr).
In the below example, a Loop is used to reverse a string in Scala.
Scala
object GFG {
def main(args: Array[String]): Unit = {
val str = "GeeksforGeeks"
var reversedStr = ""
for (i <- str.length - 1 to 0 by -1) {
reversedStr += str(i)
}
println(reversedStr)
}
}
Output:

Time Complexity: O(N)
Auxiliary Space: O(1)
Approach 3: Using Recursion
- In this approach, we define a recursive function reverseString that takes a string s as input. The function checks if the string s is empty, in which case it returns an empty string.
- Otherwise, it recursively calls itself with the tail of the string s (i.e., all characters except the first one) and appends the first character of s to the result of the recursive call.
- This process effectively reverses the string. Finally, we call reverseString with the input string str and print the reversed string using println(reversedStr).
In the below example, recursion is used to reverse a string in Scala.
Scala
object GFG {
def main(args: Array[String]): Unit = {
val str = "GeeksforGeeks"
def reverseString(s: String): String = {
if (s.isEmpty) ""
else reverseString(s.tail) + s.head
}
val reversedStr = reverseString(str)
println(reversedStr)
}
}
Output:

Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
How to reverse a string in R Reversing a string means changing its order so that the last character becomes the first, the second last character becomes the second, and so on. A while loop is a control flow statement used in R programming to execute a block of code repeatedly as long as a specified condition is true. By using a
4 min read
How to reverse a list in scala In Scala, list is defined under scala.collection.immutable package. A list is a collection of same type elements which contains immutable data. We use reverse function to reverse a list in Scala. Below are the examples to reverse a list. Reverse a simple list scala // Scala program to reverse a simp
2 min read
How to Reverse a String in Golang? Given a string and the task is to reverse the string. Here are a few examples. Approach 1: Reverse the string by swapping the letters, like first with last and second with second last and so on. Example: C // Golang program to reverse a string package main // importing fmt import "fmt" // function,
2 min read
Reverse a String in TypeScript Reversing a string involves changing the order of its characters, so the last character becomes the first, and vice versa. In TypeScript, there are various methods to achieve this, depending on the developer's preference and the specific requirements of the task. Table of Content Using a LoopUsing A
2 min read
How to Reverse keys and values in Scala Map In Scala, Map is same as dictionary which holds key:value pairs. In this article, we will learn how to reverse the keys and values in given Map in Scala. After reversing the pairs, keys will become values and values will become keys. We can reverse the keys and values of a map with a Scala for-compr
2 min read