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?
- New Content published on w3resource:
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- React - JavaScript Library
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework