Skip to contents

Takes a single string as input, reverses the characters in the string, and reassigns the reversed string to the variable in the global environment.

Usage

rev_string(string)

Arguments

string

A single string that the user wants to reverse.

Value

rev_string() does not return values to the console, because it modifies the input variable directly by assigning the value to the original input object in the global environment (i.e., with assign(envir =.GlobalEnv)). To see the result, print the original variable after calling this function.

What it does

rev_string() splits an input string into its individual characters, reverses their order, and then collapses them back into a single string. The result is then assigned to the original variable name provided as input in the global environment.

How it Works

The function uses several base R functions to achieve the reversal:

  • deparse(substitute(string)): gets the name of the input variable as a string

  • strsplit(): splits the input string into a list of single characters

  • rev(): to reverse the list

  • paste(..., collapse = ""): to combine the characters back into a single string

  • assign(): to reassign the reversed string to the original variable name in the global environment.

Examples

my_string <- "hello"
rev_string(my_string)
print(my_string) # outputs "olleh"
#> [1] "hello"