/**
 * Given a text, an index, and a snippet of text,
 * insert the snippet at the specified index position in the text.
 *
 * @param text The original text.
 * @param index The index position where the snippet should be inserted.
 * @param snippet The snippet text to be inserted.
 * @return The modified text with the snippet inserted.
 * @throws IllegalArgumentException If the index is out of the range of the text length.
 *
 * Examples:
 * insertText("Hello world!", 6, "beautiful ") -> "Hello beautiful world!"
 * insertText("Python is great!", 18, "awesome ") -> "Python is great!"
 */
fun insertText(text: String, index: Int, snippet: String): String {

    try {
        require(index >= 0) { "Index must be non-negative." }
        require(index <= text.length) { "Index is out of range." }

        return text.substring(0, index) + snippet + text.substring(index)
    } catch (e: IllegalArgumentException) {
        println("Error: ${e.message}")
        return text
    }
}
/**
 * Given a text, an index, and a snippet of text,
 * insert the snippet at the specified index position in the text.
 *
 * @param text The original text.
 * @param index The index position where the snippet should be inserted.
 * @param snippet The snippet text to be inserted.
 * @return The modified text with the snippet inserted.
 * @throws IllegalArgumentException If the index is out of the range of the text length.
 *
 * Examples:
 * insertText("Hello world!", 6, "beautiful ") -> "Hello beautiful world!"
 * insertText("Python is great!", 18, "awesome ") -> "Python is great!"
 */
fun insertText(text: String, index: Int, snippet: String): String {
/**
 * Given a text, an index, and a snippet of text,
 * insert the snippet at the specified index position in the text.
 *
 * @param text The original text.
 * @param index The index position where the snippet should be inserted.
 * @param snippet The snippet text to be inserted.
 * @return The modified text with the snippet inserted.
 * @throws IllegalArgumentException If the index is out of the range of the text length.
 *
 * Examples:
 * insertText("Hello world!", 6, "beautiful ") -> "Hello beautiful world!"
 * insertText("Python is great!", 18, "awesome ") -> "Python is great!"
 */
fun insertText(text: String, index: Int, snippet: String): String {

    try {
        require(index >= 0) { "Index must be non-negative." }
        require(index <= text.length) { "Index is out of range." }

        return text.substring(0, index) + snippet + text.substring(index)
    } catch (e: IllegalArgumentException) {
        println("Error: ${e.message}")
        return text
    }
}