Scala Programming: Append two given strings such that, if the concatenation creates a double characters then omit one of the characters
Scala Programming String Exercise-19 with Solution
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
Scala Code Editor :
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: 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.
What is the difficulty level of this exercise?
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/scala-exercises/string/scala-string-exercise-19.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics