11  Remove First and Last Character

8kyu Tantangan #11/366 - 25 Feb 2024

https://www.codewars.com/kata/56bc28ad5bdaeb48760009b0

11.1 Instruction

It’s pretty straightforward. Your goal is to create a function that removes the first and last characters of a string. You’re given one parameter, the original string. You don’t have to worry about strings with less than two characters.

11.2 YouTube Video

11.3 Solution Code

removeChar <- function(str){
  char <- unlist(strsplit(str, ""))
  char_removed <- char[-c(1,length(char))]
  paste(char_removed, collapse = "")
}
removeChar <- function(str){
    substr(str, 2, nchar(str)-1)
}

11.4 Test

library(testthat)

test_that('basic tests', {
  expect_equal(removeChar('eloquent'), 'loquen')
  expect_equal(removeChar('country'), 'ountr')
  expect_equal(removeChar('person'), 'erso')
  expect_equal(removeChar('place'), 'lac')
})
Test passed 🥇

11.5 Supported by

StarCore Analytics