String Manipulation – Exercises

November 27, 2016

(This article was first published on R-exercises, and kindly contributed to R-bloggers)


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 on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


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)