Back to Blog

Kotlin with Copilot: Add "!" to the end of a sentence

Sandy LaneSandy Lane

Video: Kotlin with Copilot: Add "!" to the end of a sentence by Taught by Celeste AI - AI Coding Coach

Watch full page →

Kotlin with Copilot: Add "!" to the End of a Sentence

In Kotlin, appending an exclamation mark to the end of a sentence is straightforward and can be done using simple string concatenation. This example demonstrates how to take a given sentence and add an exclamation mark to emphasize it, which is a common task in text manipulation.

Code

fun addExclamation(sentence: String): String {
  // Append "!" to the input sentence
  return sentence + "!"
}

fun main() {
  val original = "Hello, Kotlin"
  val excited = addExclamation(original)
  println(excited)  // Output: Hello, Kotlin!
}

Key Points

  • String concatenation in Kotlin is done using the + operator.
  • Functions can be used to encapsulate common string operations for reuse.
  • Kotlin's type system ensures the input and output are both Strings in this example.
  • Printing the result shows the modified sentence with the exclamation mark appended.