Scala Programming: Append two given strings such that, if the concatenation creates a double characters then omit one of the characters
Write a Scala program to append two given strings such that, if the concatenation creates double characters then omit one of the characters.
Sample Solution:
Scala Code:
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
Go to:
PREV : Write a Scala program to check if two given strings are rotations of each other.
NEXT : Write a Scala program to create a new string from a given string swapping the last two characters of the given string. The length of the given string must be two or more.
Scala Code Editor :
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
