Cookie Preferences

We use cookies to enhance your experience. Choose your preference for cookie usage.

Essential cookies are required for basic functionality. Additional cookies help us improve our service and provide analytics.

View third-party services
  • Google Analytics: Website traffic analysis and conversion tracking
  • RudderStack: Analysis of user interactions with the website
  • Microsoft Clarity: User behavior analysis & session recordings
  • Facebook Pixel: Marketing analysis of user activity

Splitting with spaces

Report a typo

Write a program that takes one string and the number of substrings to split it. The program should split the string into substrings using any number of spaces (1 or more) as a separator.

Input: string: String, n: Int

Output: resulting n substrings, each on a new line.

Sample Input 1:

This   is a      text with   many     spaces
0

Sample Output 1:

This
is
a
text
with
many
spaces

Sample Input 2:

Lorem    ipsum dolor sit, amet,    consectetur
3

Sample Output 2:

Lorem
ipsum
dolor sit, amet,    consectetur
Write a program in Kotlin
fun main() {
val string = readLine()!!
val n = readLine()!!.toInt()

}
___

Create a free account to access the full topic