R/reverse.R

Defines functions string_reverse auto_split

Documented in string_reverse

#' Reverses a string or number
#' @param x A string or a number
#' @param convert Flag to set whether the input number will be converted to strings or not
#' @return the reverse of input
#' @examples
#' string_reverse(1234)
#' string_reverse("1234")
#' @importFrom magrittr "%>%"
#' @export
string_reverse <- function(x, convert = TRUE)
{
  splits <- auto_split(x)
  r <- rev(splits) %>% paste(collapse = "")
  if(convert)
  {
    tryCatch(as.numeric(r), warning = function(c) return (r))
  }
  else
  {
    r
  }
}

auto_split <- function(x)
{
  return (strsplit(as.character(x), "")[[1]])
}
hinduBale/testReverserPackage documentation built on Jan. 4, 2020, 12:17 a.m.