In R, you can sort characters in a string using the strsplit()
and paste()
functions.
In this example,
str
.strsplit()
function.paste()
function.str <- 'hello world'
sorted_str <- paste(sort(strsplit(str, '')[[1]]), collapse='')
print(paste('Sorted string in ascending order:', sorted_str))
Sorted string in ascending order: dehllloorw
In this example,
str
.strsplit()
function.paste()
function.str <- 'hello world'
sorted_str <- paste(sort(strsplit(str, '')[[1]], decreasing = TRUE), collapse='')
print(paste('Sorted string in descending order:', sorted_str))
Sorted string in descending order: wroolllhed
In this tutorial, we learned How to Sort Characters in String in R language with well detailed examples.