Fork me on GitHub

Introduction to Strings with SARL

Note If you don’t know how to solve an problem, or what is the function to be used, you could search on Internet for the answer using the API of the Java programming language. Indeed, since SARL is fully compatible with the Java API, you could use all the types or functions that are defined in this Java API.

1. Exercise 1

Write a SARL program to calculate the length of a string, providing on the command line.

Solution

2. Exercise 2

Write a SARL program to count the number of characters (character frequency) in a string.

Solution

3. Exercise 3

Write a SARL program to get a string made of the first 2 and last 2 characters of a given string (from the command line). If the string length is less than 2, return the empty string instead.

Solution

4. Exercise 4

Write a SARL program to get a string from a given string (from the command line) where all occurrences of its first char have been changed to *, except the first char itself.

Solution

5. Exercise 5

Write a SARL program to get a single string from two given strings (from the command line), separated by a space and swap the first two characters of each string.

Solution

6. Exercise 6

Write a SARL program to add ing at the end of a given string (length should be at least 3). If the given string already ends with ing, add ly instead. If the string length of the given string is less than 3, leave it unchanged.

Solution

7. Exercise 7

Write a SARL program to find the first occurrence of the substrings not and poor in a given string. If not follows poor, replace the whole notpoor substring with good. Return the resulting string.

Solution

8. Exercise 8

Write a SARL function that takes a list of words and return the longest word and the length of the longest one.

Solution

9. Exercise 9

Write a SARL program to remove the nth index character from a nonempty string.

Solution

10. Exercise 10

Write a SARL program to change a given string to a newly string where the first and last chars have been exchanged.

Solution

11. Exercise 11

Write a SARL program to remove characters that have odd index values in a given string.

Solution

12. Exercise 12

Write a SARL program to count the occurrences of each word in a given sentence.

Solution

13. Exercise 13

Write a SARL script that takes input from the command line and displays that input back in upper and lower cases.

Solution

14. Exercise 14

Write a SARL program that accepts a comma-separated sequence of words as command line input and prints the distinct words in sorted form (alphanumerically).

Solution

15. Exercise 15

Write a SARL function to create an HTML string with tags around the word(s). Sample function and result:

Solution

16. Exercise 16

Write a SARL function to insert a string in the middle of a string. Sample function and result :

Solution

17. Exercise 17

Write a SARL function to get a string made of 4 copies of the last two characters of a specified string (length must be at least 2). Sample function and result :

Solution

18. Exercise 18

Write a SARL function to get a string made of the first three characters of a specified string. If the length of the string is less than 3, return the original string. Sample function and result :

Solution

19. Exercise 19

Write a SARL function to reverse a string if its length is a multiple of 4.

Solution

20. Exercise 20

Write a SARL function to convert a given string to all uppercase if it contains at least 2 uppercase characters in the first 4 characters.

Solution

21. Exercise 21

Write a SARL program to sort a string lexicographically.

Solution

22. Exercise 22

Write a SARL program to remove a newline in a string.

Solution

23. Exercise 23

Write a SARL program to check whether a string starts with specified characters.

Solution

24. Exercise 24

Write a SARL program to create a Caesar encryption.

Note In cryptography, a Caesar cipher, also known as Caesar’s cipher, the shift cipher, Caesar’s code or Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be replaced by A, E would become B, and so on. The method is named after Julius Caesar, who used it in his private correspondence.

Solution

25. Exercise 25

Write a SARL program to remove existing indentation from all of the lines in a given text.

Solution

26. Exercise 26

Write a SARL program to add prefix text to all of the lines in a string.

Solution

27. Exercise 27

Write a SARL program to print the real numbers up to 2 decimal places.

Solution

28. Exercise 28

Write a SARL program to print the real numbers up to 2 decimal places with a sign.

Solution

29. Exercise 29

Write a SARL program to print the real positive and negative numbers with no decimal places.

Solution

30. Exercise 30

Write a SARL program to print the integers with zeros to the left of the specified width.

Solution

31. Exercise 31

Write a SARL program to print the integers with ‘*’ to the right of the specified width.

Solution

32. Exercise 32

Write a SARL program to count occurrences of a substring in a string, both provided on the command line.

Solution

33. Exercise 33

Write a SARL program to reverse a string that is provided on the command line.

Solution

34. Exercise 34

Write a SARL program to reverse words in a string that is provided on the command line.

Solution

35. Exercise 35

Write a SARL program to print the square and cube symbols in the area of a rectangle and the volume of a cylinder, using the string formatting tool of the API.

area = 1256.66
volume = 1254.725
decimals = 2
The area of the rectangle is 1256.66cm2
The volume of the cylinder is 1254.725cm3

Solution

36. Exercise 36

Write a SARL program to print the index of a character in a string.

Current character w position at 0
Current character 3 position at 1
Current character r position at 2
....
Current character c position at 8
Current character e position at 9

Solution

37. Exercise 37

Write a SARL program to check whether a string contains all letters of the alphabet.

Solution

38. Exercise 38

Write a SARL program to convert a given string into a list of words.

['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog.']

Solution

39. Exercise 39

Write a SARL program to lowercase the first n characters in a string.

Solution

40. Exercise 40

Write a SARL program to swap commas and dots in a string.

Solution

41. Exercise 41

Write a SARL program to count and display vowels in text.

Solution

42. Exercise 42

Write a SARL program to split a string on the last occurrence of the delimiter.

Solution

43. Exercise 43

Write a SARL program to find the first non-repeating character in a given string.

Solution

44. Exercise 44

Write a SARL program to print all permutations with a given repetition number of characters of a given string.

Solution

45. Exercise 45

Write a SARL program to find the first repeated character in a given string.

Solution

46. Exercise 46

Write a SARL program to find the first repeated character in a given string where the index of the first occurrence is smallest.

Solution

47. Exercise 47

Write a SARL program to find the first repeated word in a given string.

Solution

48. Exercise 48

Write a SARL program to find the second most repeated word in a given string.

Solution

49. Exercise 49

Write a SARL program to remove spaces from a given string.

Solution

50. Exercise 50

Write a SARL program to find all the common characters in lexicographical order from two given lower case strings. If there are no similar letters print No common characters.

Solution

51. Exercise 51

Write a SARL program to make two given strings (lower case, may or may not be of the same length) anagrams without removing any characters from any of the strings.

Solution

52. Exercise 52

Write a SARL program to remove all consecutive duplicates of a given string.

Solution

53. Exercise 53

Write a SARL program to find the longest common sub-string from two given strings.

Solution

54. Exercise 54

Write a SARL program to count Uppercase, Lowercase, special characters and numeric values in a given string.

Solution

55. Exercise 55

Write a SARL program to wrap a given string into a paragraph with a given width.

The quick
brown fox.

Solution

56. Exercise 56

Write a SARL program to swap cases in a given string.

Solution

57. Exercise 57

Write a SARL program to check whether a given string contains a capital letter, a lower case letter, a number and a minimum length.

Solution

58. Exercise 58

Write a SARL program to convert a given heterogeneous list of scalars into a string. Sample Output:

Solution

59. Exercise 59

Write a SARL program to extract numbers from a given string.

Solution

60. Exercise 60

Write a SARL program to replace each character of a word of length five and more with a hash character (#).

Solution

Copyright © 2014-2023 SARL.io, the Original Authors and Main Authors.

Documentation text and medias are licensed under the Creative Common CC-BY-SA-4.0; you may not use this file except in compliance with CC-BY-SA-4.0. You may obtain a copy of CC-BY-4.0.

Examples of SARL code are licensed under the Apache License, Version 2.0; you may not use this file except in compliance with the Apache License. You may obtain a copy of the Apache License.

You are free to reproduce the content of this page on copyleft websites such as Wikipedia.

Generated with the translator docs.generator 0.14.0-SNAPSHOT.