/**
 * You are a psychologist and you would like to analyze the text responses from your patients. Your task is to write a function that slices and replaces certain elements in a given string multiple times.
 *
 * @param s A string containing the text responses from patients.
 * @param pattern The pattern to be replaced in the string.
 * @param replacement The replacement for the pattern.
 * @param n The number of times the replacement should occur.
 * @return The modified string after performing the slicing and replacing.
 *
 * Example:
 * sliceAndReplace("I feel happy and happy today.", "happy", "sad", 2)
 * Output: "I feel sad and sad today."
 */
fun sliceAndReplace(s: String, pattern: String, replacement: String, n: Int): String {

    var modifiedString = s
    repeat(n) {
        val startIndex = modifiedString.indexOf(pattern)
        val endIndex = startIndex + pattern.length
        modifiedString = modifiedString.substring(0, startIndex) + replacement + modifiedString.substring(endIndex)
    }
    return modifiedString
}
/**
 * You are a psychologist and you would like to analyze the text responses from your patients. Your task is to write a function that slices and replaces certain elements in a given string multiple times.
 *
 * @param s A string containing the text responses from patients.
 * @param pattern The pattern to be replaced in the string.
 * @param replacement The replacement for the pattern.
 * @param n The number of times the replacement should occur.
 * @return The modified string after performing the slicing and replacing.
 *
 * Example:
 * sliceAndReplace("I feel happy and happy today.", "happy", "sad", 2)
 * Output: "I feel sad and sad today."
 */
fun sliceAndReplace(s: String, pattern: String, replacement: String, n: Int): String {
/**
 * You are a psychologist and you would like to analyze the text responses from your patients. Your task is to write a function that slices and replaces certain elements in a given string multiple times.
 *
 * @param s A string containing the text responses from patients.
 * @param pattern The pattern to be replaced in the string.
 * @param replacement The replacement for the pattern.
 * @param n The number of times the replacement should occur.
 * @return The modified string after performing the slicing and replacing.
 *
 * Example:
 * sliceAndReplace("I feel happy and happy today.", "happy", "sad", 2)
 * Output: "I feel sad and sad today."
 */
fun sliceAndReplace(s: String, pattern: String, replacement: String, n: Int): String {

    var modifiedString = s
    repeat(n) {
        val startIndex = modifiedString.indexOf(pattern)
        val endIndex = startIndex + pattern.length
        modifiedString = modifiedString.substring(0, startIndex) + replacement + modifiedString.substring(endIndex)
    }
    return modifiedString
}