Write a Scala program to append two given strings such that, if the concatenation creates double characters then omit one of the characters.
object Scala_String { def conCat(str1: String, str2: String): String = { if (str1.length != 0 && str2.length != 0 && str1.charAt(str1.length() - 1) == str2.charAt(0)) return str1 + str2.substring(1); return str1 + str2; } def main(args: Array[String]): Unit = { val str1 = "food"; val str2 = "door"; println("The given strings are: " + str1 + " and " + str2); println("The string after concatination are: " + conCat(str1, str2)); } }
Sample Output:
The given strings are: food and door The string after concatination are: foodoor
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Sample Output:
need an explanation for this answer? contact us directly to get an explanation for this answer