In the R Programming language, the functions casefold()
, toupper()
& tolower()
are string functions used to change the case of letters in a string.
The function casefold()
can be used for both, changing upper case letters to lower case & lower case letters to upper case. Whereas toupper()
can only change lowercase letters to uppercase and tolower()
can only change uppercase letters to lowercase. These functions work for only alphabets. In case there is a non-alphabet character in the string, it will remain the same as it was before passing to the function.
casefold()
Function in R
By default, casefold()
act as the tolower()
function, i.e., it changes the case of all uppercase letters in a string to lowercase letters.
Code Example 1 (Default Implementation)
x <- "WelCome to AlgoIdeas"
x <- casefold(x)
print(x)
- Line 1: Assigning a string containing both upper & lowercase letters.
- Line 2: Invoking
casefold()
function on the string & storing the returned string in the original variablex
. - Line 3: Printing the updated string on console.
Output

Moreover, along with the string argument, the casefold()
function can accept one Boolean argument passed as casefold(name_of_string, upper=FALSE)
. If you pass upper=FALSE
, it will act the same as tolower()
function. But, if you want to capitalize the lowercase letters, you must pass upper=TRUE
along with the string argument. The below example will help you better understand this function.
Code Example 2(Passing the Boolean argument)
x <- "Welcome to AlgoIdeas"
x <- casefold(x, upper=TRUE)
print(x)
- Line 1: Assigning a string containing both upper & lowercase letters.
- Line 2: Invoking
casefold()
function on the string while setting the upper parameter toTRUE
& storing the returned string in variablex
. - Line 3: Printing the updated string on console.
Output

toupper()
Function in R
The toupper()
function takes a string as input and capitalizes all its Lowercase letters to Uppercase.

x <- toupper("WelCome to AlgoIdeas")
print(x)
- Line 1: Passing a string containing both upper & lowercase letters to the
toupper()
function and assigning the returned value to variablex
. - Line 2: Printing the returned string.
Output

tolower()
Function in R
The tolower()
function takes a string as input and changes the case of all its uppercase letters to lowercase letters.

x <- tolower("WelCome to AlgoIdeas")
print(x)
- Line 1: Passing a string containing both upper & lowercase letters to the
tolower()
function and assigning the returned value to variablex
. - Line 2: Printing the returned string.
Output
