Q:

Write a Scala program to append two given strings such that, if the concatenation creates double characters then omit one of the characters

0

Write a Scala program to append two given strings such that, if the concatenation creates double characters then omit one of the characters.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now