/**
 * Given a sentence and a word, this function searches for the word in the sentence and returns the number of occurrences found.
 * However, the exercise requires adding an if statement to skip counting any occurrences of the word that are followed by a specific character '!'.
 *
 * @param sentence A string representing the sentence to search within.
 * @param word A string representing the word to search for.
 * @return An integer representing the total number of occurrences of the word in the sentence, excluding occurrences followed by '!'.
 */
fun findWordsInSentence(sentence: String, word: String): Int {

    var count = 0
    var index = 0

    while (index < sentence.length) {
        if (sentence.substring(index).startsWith(word)) {
            count++
            index += word.length
            if (index < sentence.length && sentence[index] == '!') {
                // Skip counting this occurrence since it is followed by '!'
                index++
            }
        } else {
            index++
        }
    }

    return count
}

// To test the function, uncomment the lines below:
// val sentence = "The journalist saw a dog jumping over the fence!"
// val word = "dog"
// println(findWordsInSentence(sentence, word))
// Output:
/**
 * Given a sentence and a word, this function searches for the word in the sentence and returns the number of occurrences found.
 * However, the exercise requires adding an if statement to skip counting any occurrences of the word that are followed by a specific character '!'.
 *
 * @param sentence A string representing the sentence to search within.
 * @param word A string representing the word to search for.
 * @return An integer representing the total number of occurrences of the word in the sentence, excluding occurrences followed by '!'.
 */
fun findWordsInSentence(sentence: String, word: String): Int {
/**
 * Given a sentence and a word, this function searches for the word in the sentence and returns the number of occurrences found.
 * However, the exercise requires adding an if statement to skip counting any occurrences of the word that are followed by a specific character '!'.
 *
 * @param sentence A string representing the sentence to search within.
 * @param word A string representing the word to search for.
 * @return An integer representing the total number of occurrences of the word in the sentence, excluding occurrences followed by '!'.
 */
fun findWordsInSentence(sentence: String, word: String): Int {

    var count = 0
    var index = 0

    while (index < sentence.length) {
        if (sentence.substring(index).startsWith(word)) {
            count++
            index += word.length
            if (index < sentence.length && sentence[index] == '!') {
                // Skip counting this occurrence since it is followed by '!'
                index++
            }
        } else {
            index++
        }
    }

    return count
}

// To test the function, uncomment the lines below:
// val sentence = "The journalist saw a dog jumping over the fence!"
// val word = "dog"
// println(findWordsInSentence(sentence, word))
// Output: