Scala Programming String - Exercises, Practice, Solution

Scala Programming String [46 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts.]

1. Write a Scala program to get the character at the given index within a given String. Also print the length of the string. Go to the editor
Sample Output:

```Original String = Scala Exercises!
The character at position 0 is S
The character at position 10 is c
The character at position 15 is !
Length of the string: 16
```
Click me to see the sample solution

2. Write a Scala program to get the character (Unicode code point) at the given index within the String. Go to the editor
Sample Output:

```Original String : w3resource - Scala
Character(unicode point) = 51
Character(unicode point) = 101
```
Click me to see the sample solution

3. Write a Scala program to compare two strings lexicographically. Go to the editor
Note: Two strings are lexicographically equal if they are the same length and contain the same characters in the same positions
Sample Output:

```ring 1: This is Exercise 1
String 2: This is Exercise 2
"This is Exercise 1" is less than "This is Exercise 2"
========================
String 1: This is Exercise 1
String 2: This is Exercise 1
"This is Exercise 1" is equal to "This is Exercise 1"
========================
String 1: This is Blacky
String 2: This is Black
"This is Blacky" is greater than "This is Black"
```
Click me to see the sample solution

4. Write a Scala program to concatenate a given string to the end of another string. Go to the editor
Sample Output:

```Original strings:
String 1: Scala Exercises and
String 2: Python Exercises
The concatenated string: Scala Exercises and Python Exercises
```
Click me to see the sample solution

5. Write a Scala program to test if a given string contains the specified sequence of char values. Go to the editor
Sample Output:

```Original String: Scala Exercises and Python Exercises
Specified sequence of char values: and
Test if the said string contains the specified sequence of char values!
true
Original String: Scala Exercises and PHP Exercises
Specified sequence of char values: Python
Test if the said string contains the specified sequence of char values!
false
```
Click me to see the sample solution

6. Write a Scala program to create a new String object with the contents of a character array. Go to the editor
Sample Output:

```The book contains 345 pages.
```
Click me to see the sample solution

7. Write a Scala program to check whether a given string ends with the contents of another string. Go to the editor
Sample Output:

```"Python Exercises" ends with "se"? false
"Python Exercise" ends with "se"? true
```
Click me to see the sample solution

8. Write a Scala program to check whether two String objects contain the same data. Go to the editor
Sample Output:

```"Stephen Edwin King" equals "Stephen Edwin  King"? false
"Stephen Edwin King" equals "Stephen Edwin King"? true
```
Click me to see the sample solution

9. Write a Scala program to compare a given string to another string, ignoring case considerations. Go to the editor
Sample Output:

```"Stephen Edwin King" equals "Stephen Edwin  King"? false
"Stephen Edwin King" equals "Stephen edwin king"? true
```
Click me to see the sample solution

10. Write a Scala program to replace a specified character with another character. Go to the editor
Sample Output:

```Original string: The quick brown fox jumps over the lazy dog.
New String: The quick brown fox jumps over the lazy fog.
```
Click me to see the sample solution

11. Write a Scala program to get a substring of a given string between two specified positions. Go to the editor
Sample Output:

```Original = The quick brown fox jumps over the lazy dog.
From said string, substring between two positions (10,26) = brown fox jumps
```
Click me to see the sample solution

12. Write a Scala program to convert all the characters to lowercase, uppercase strings. Go to the editor
Sample Output:

```Original String: The Quick BroWn FoX!
String in lowercase: the quick brown fox!
String in uppercase: THE QUICK BROWN FOX!
```
Click me to see the sample solution

13. Write a Scala program to trim any leading or trailing whitespace from a given string. Go to the editor
Sample Output:

```Original String:- Scala Exercises
New String:-Scala Exercises
```
Click me to see the sample solution

14. Write a Scala program to print after removing duplicates from a given string. Go to the editor
Sample Output:

```The given string is: w3resource
After removing duplicates characters the new string is: w3resouc
The given string is: Scala
After removing duplicates characters the new string is: Scal
The given string is: 2q34u923u4928402
After removing duplicates characters the new string is: 2q34u980
```
Click me to see the sample solution

15. Write a Scala program to find the maximum occurring character in a string. Go to the editor
Sample Output:

```The given string is: test string
Maximum occurring character of the said string is: t
The given string is: Scala
Maximum occurring character of the said string is: a
```
Click me to see the sample solution

16. Write a Scala program to reverse every word in a given string. Go to the editor
Sample Output:

```The given string is: This is a test string
The new string after reversed the words: sihT si a tset gnirts
The given string is: The Scala Programming Language
The new string after reversed the words: ehT alacS gnimmargorP egaugnaL
```
Click me to see the sample solution

17. Write a Scala program to count and print all the duplicates in the input string. Go to the editor
Sample Output:

```The given string is: w3resource
The duplicate characters and counts are:
e  appears  2  times
r  appears  2  times
The given string is: The Scala Programming Language
The duplicate characters and counts are:
appears  3  times
a  appears  5  times
e  appears  2  times
g  appears  4  times
m  appears  2  times
n  appears  2  times
r  appears  2  times
```
Click me to see the sample solution

18. Write a Scala program to check if two given strings are rotations of each other. Go to the editor
Sample Output:

```The given strings are: ABACD  and  CDABA
The concatination of 1st string twice is: ABACDABACD
The 2nd string CDABA  exists in the new string.
Strings are rotations of each other
```
Click me to see the sample solution

19. Write a Scala program to append two given strings such that, if the concatenation creates double characters then omit one of the characters. Go to the editor
Sample Output:

```The given strings are: food  and  door
The string after concatination are: foodoor
```
Click me to see the sample solution

20. 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. Go to the editor
Sample Output:

```The given strings is: String
The string after swap last two characters are: Strign
The given strings is: Scala
The string after swap last two characters are: Scaal
```
Click me to see the sample solution

21. Write a Scala program to read a string and return true if it ends with a specified string of length 2. Go to the editor
Sample Output:

```The given strings is: String
The string containing ng at last: true
The given strings is: String
The string containing gn at last: false
```
Click me to see the sample solution

22. Write a Scala program to read two strings append them together and return the result. If the length of the strings is different remove characters from the beginning of longer string and make them equal length. Go to the editor
Sample Output:

```The given strings is: Welcome and home
The new string is: comehome
The given strings is: Scala and Python
The new string is: Scalaython
```
Click me to see the sample solution

23. Write a Java program to create a new string taking specified number of characters from first and last position of a given string. Go to the editor
Sample Output:

```The given strings is: Welcome
The given numbers is: 3
The new string is: Welome
The given strings is: Scala Programming
The given numbers is: 4
The new string is: Scalming
```
Click me to see the sample solution

24. Write a Scala program to check whether the first two characters present at the end of a given string. Go to the editor
Sample Output:

```The given strings is: educated
If first two characters appear in the last! true
The given strings is: ABCDEFBA
If first two characters appear in the last! false
```
Click me to see the sample solution

25. Write a Scala program to read a given string and if the first or last characters are same return the string without those characters otherwise return the string unchanged. Go to the editor
Sample Output:

```The given strings is: testcricket
The new string is: estcricke
The given strings is: testcricket
The new string is: testcricket
```
Click me to see the sample solution

26. Write a Scala program to read a string and return the string without the first two characters. Keep the first char if it is 'g' and keep the second char if it is 'h'. Go to the editor
Sample Output:

```The given strings is: ghost
The new string is: ghost
The given strings is: photo
The new string is: hoto
The given strings is: goat
The new string is: gat
```
Click me to see the sample solution

27. Write a Scala program to read a string and if one or both of the first two characters is equal to specified character return without those characters otherwise return the string unchanged. Go to the editor
Sample Output:

```The given strings is: aacyte, specified character is: a
The new string is: cyte
The given strings is: bacyte, specified character is: a
The new string is: bcyte
The given strings is: bbacyte, specified character is: a
The new string is: bbacyte
```
Click me to see the sample solution

28. Write a Scala program to read a string and returns after remove a specified character and its immediate left and right characters. Go to the editor
Sample Output:

```The given strings is: test#string
The new string is: testring
The given strings is: sdf\$#gyhj#
The new string is: sdgyhj#
```
Click me to see the sample solution

29. Write a Scala program to check two given strings whether any one of them appear at the end of the other string (ignore case sensitivity). Go to the editor
Sample Output:

```The given strings are: pqrxyz  and xyz
Is one string appears at the end of other? true
The given strings are: pqrxyz  and rxy
Is one string appears at the end of other? false
```
Click me to see the sample solution

30. Write a Scala program to check whether a substring appears before a period(.) within a given string. Go to the editor
Sample Output:

```The given string is: testabc.test
Is abc appear before a period in the said string? true
The given string is: test.abctest
Is abc appear before a period in the said string? false
```
Click me to see the sample solution

31. Write a Scala program to check whether a prefix string creates using the first specific characters in a given string appears somewhere else in the string. Go to the editor
Sample Output:

```The given string is: MrsJemsMrsam
The prefix string length is: 3
Is 'Mrs' appear else where in the string? true
The given string is: MrJemsam
The prefix string length is: 2
Is 'Mr' appear else where in the string? false
```
Click me to see the sample solution

32. Write a Scala program to check whether a given substring presents in the middle of another given string. Here middle means difference between the number of characters to the left and right of the given substring not more than 1. Go to the editor
Sample Output:

```The given string is: wwabcwww
Is abc appear in middle? true
The given string is: wwwabcwww
Is abc appear in middle? true
The given string is: wwwwabcwww
Is abc appear in middle? true
The given string is: wwwwwabcwww
Is abc appear in middle? false
```
Click me to see the sample solution

33. Write a Scala program to count how many times the substring 'life' present at anywhere in a given string. Counting can also happen for the substring 'li?e',any character instead of 'f'. Go to the editor
Sample Output:

```The given string is: live on wild life
The substring life or li?e appear number of times: 2
```
Click me to see the sample solution

34. Write a Scala program to add a string with specific number of times separated by a substring. Go to the editor
Sample Output:

```The given strings are: try  and  best
Number to times to be repeat: 3
The new string is: trybesttrybesttry
```
Click me to see the sample solution

35. Write a Scala program to repeat a specific number of characters for specific number of times from the last part of a given string. Go to the editor
Sample Output:

```The given string is: string
The new string after repetition: inginging
```
Click me to see the sample solution

36. Write a Scala program to create a new string from a given string after removing the 2nd character from the substring of length three starting with 'z' and ending with 'g' presents in the said string. Go to the editor
Sample Output:

```The given string is: zzgkitandkatcaketoket
The new string is: zgkitandkatcaketoket
The given string is: kitandkazzgtcaketoket
The new string is: kitandkazgtcaketoket
The given string is: kitandkatcaketoketzzg
The new string is: kitandkatcaketoketzg
```
Click me to see the sample solution

37. Write a Scala program to check whether the character immediately before and after a specified character is same in a given string. Go to the editor
Sample Output:

```The given string is: moon#night and the specified character is: #
The before and after # both characters are same in the said string: true
The given string is:  bat#\$#ball and the specified character is: \$
The before and after \$ both characters are same in the said string: true
The given string is:  bat#\$ball and the specified character is: \$
The before and after \$ both characters are same in the said string: false
```
Click me to see the sample solution

38. Write a Java program to check whether two strings of length 3 and 4 appear in same number of times in a given string. Go to the editor
Sample Output:

```The original string is: redcapmanwithbluecar
Searched strings are: blue,red
The appearance of red and blue are same: true
The original string is: redcapmanwithbluecarblue
Searched strings are: blue,red
The appearance of red and blue are same: false
```
Click me to see the sample solution

39. Write a Scala program to create a new string repeating every character twice of a given string. Go to the editor
Sample Output:

```The given string is: welcome
The new string is: wweellccoommee
```
Click me to see the sample solution

40. Write a Scala program to make a new string from two given string in such a way that, each character of two string will come respectively. Go to the editor
Sample Output:

```The given strings  are: welcome  and  w3resource
The new string is: wwe3lrceosmoeurce
```
Click me to see the sample solution

41. Write a Scala program to make a new string made of p number of characters from the first of a given string and followed by p-1 number characters till the p is greater than zero. Go to the editor
Sample Output:

```The given string is: welcome
Number of repetition characters and repetition: 4
The new string is: welcwelwew
```
Click me to see the sample solution

42. Write a Scala program to make a new string with each character of just before and after of a non-empty substring whichever it appears in a non-empty given string. Go to the editor
Sample Output:

```The given string are: weablcoabmeab  and ab
The new string is: elome
```
Click me to see the sample solution

43. Write a Scala program to count the number of triples (characters appearing three times in a row) in a given string. Go to the editor
Sample Output:

```The given string is: welllcommmmeee
The number of triples in the string is: 4
```
Click me to see the sample solution

44. Write a Scala program to check whether a specified character is happy or not. A character is happy when the same character appears to its left or right in a string. Go to the editor
Sample Output:

```The given string is: azzlea
Is z happy in the said string: true
The given string is: abcfdkefg
Is f happy in the said string: false
```
Click me to see the sample solution

45. Write a Scala program to calculate the sum of the numbers appear in a given string. Go to the editor
Sample Output:

```The given string is: it 15 is25 a 20string
The sum of the numbers in the said string is: 60
```
Click me to see the sample solution

46. Write a Java program to check the number of appearances of the two substrings appear anywhere in the string. Go to the editor
Sample Output:

```The given string is: Thisisthethesis
Are the appearance of 'the' and 'is' equal? false
The given string is: Thisisthethes
Are the appearance of 'the' and 'is' equal? true
```
Click me to see the sample solution

More...

Scala Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

Test your Python skills with w3resource's quiz

﻿