String Manipulation – Exercises

[This article was first published on R-exercises, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.


In this set of exercises we will practice functions that enable us to manipulate strings.You can find more about string manipulation functions in Handling and Processing Strings in R e-book.

Answers to the exercises are available here.

If you have different solution, feel free to post it.

Exercise 1

Load text from the file and print it on screen. Text file contains excerpt from novel “Gambler” by Fyodor Dostoyevsky.

Exercise 2

How many paragraphs is there in the excerpt?

Exercise 3

How many characters is there in the excerpt

Exercise 4

Collapse paragraphs into one and display it on the screen (un-list it).

Exercise 5

Convert the text to uppercase and save it to new file “gambler-upper.txt”.

Exercise 6

Change all letters ‘a’ and ‘t’ to ‘A’ and ‘T’.

Exercise 7

Does the text contain word ‘lucky’?

Exercise 8

How many words are there in the excerpt, assuming that words are sub-strings separated by space or new line character?

Exercise 9

How many times is word money repeated in the excerpt?

Exercise 10

Ask the user to input two numbers, divide them and display both numbers and the result on the screen, each of them formatted to 2 decimal places.

To leave a comment for the author, please follow the link and comment on their blog: R-exercises. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)