E002: Manipulating strings

Author

Hubert Baechli

Published

March 8, 2025

Programming exercise

Below are some exercises that should help you learn the most important basic built in functions and how to incorporate them into your own functions. This sheet focuses on letters and strings.

Note

In the exercises below, the symbol GAP is a placeholder. You should replace it with appropriate commands to solve the problem.

1. Hello world

Let’s start with the obligatory ‘Hello World’.

greet <- function() {
  print("Hello world")
}
greet()
[1] "Hello world"

2. Personal Greetings (easy)

Since the first example was very impersonal, now write a function that uses your name.

greet_person <- function(name) {
  text <- paste(GAP, name, sep = GAP)
  print(text)
}

It should output something with it.

greet_person("Hubi")
[1] "Hello Hubi"

3. Fancy Greetings (medium)

To make it a bit more exciting, the next function should output something different depending on the time of day. You should use the if and else statements we learned in class to complete the assignment.

Here is a helper function to determine the current hour of the day:

current_hour <- function() {
  # get the current time from the machine 
  current_time <- Sys.time()

  # extract the hour from the full time string 
  hour_string <- format(current_time, "%H")

  # convert to a number and return 
  # return() is unnecessary: functions return the value of the last expression
  as.numeric(hour_string) 
}

You can use it like this:

current_hour()
[1] 22

Fill out the body of the function below so that the function should print ‘Good morning’ until 12, and after that, print ‘Good afternoon’:

greet_time <- function(name, hour = current_hour()) {
  # put code here
}

By default the function should use the current time, but to test it you can give it directly the hour as an argument:

greet_time("Hubi")
[1] "Good afternoon Hubi"
greet_time("Hubi", hour = 11)
[1] "Good morning Hubi"
greet_time("Hubi", hour = 13)
[1] "Good afternoon Hubi"

4. Very fancy Greetings (hard)

If the last exercise wasn’t fancy enough, you can extend it as follows:

if (condition1) {
  # something happens here if condition1 is TRUE
} else if (condition2) {
  # something else happens here if condition1 is FALSE but condition2 is TRUE
} else {
  # a final thing happens if both condition1 and condition2 are FALSE
}

May something like that ;-)

very_fancy_greet("Hubi", hour = 23)
[1] "Go to bed Hubi"

5. Reverse Strings (hard)

Goal: Write a function which reverses a string and makes all letters uppercase.

Your function should do this

rev_string("Hubi")
[1] "IBUH"

Here are some tips. A very useful function that you might use at some point during data preparation is this:

words <- strsplit("Hello World", split = " ")

The result is here a List of words

words
[[1]]
[1] "Hello" "World"
words[[1]] # vector
[1] "Hello" "World"

You can also use this for a own function that reverses a string.

hint: you may also use following built in functions from R

toupper("text")
[1] "TEXT"
rev(c(1, 2, 3))
[1] 3 2 1

Combine the functions above in order to complete assignment.

6. Palindrome (combined)

If you’ve mastered the last exercise, it’s just a small step to write a function that checks if a word is a palindrome.

Your function should give out TRUE if the word is a palindrome.

Hint: You can use your function “rev_string” inside the new function

is_palindrome("Hubi")
[1] FALSE
is_palindrome("Anna")
[1] TRUE