w3resource

Ruby String Exercises: Sort a string's characters alphabetically

Ruby String: Exercise-11 with Solution

Write a Ruby program to sort a string's characters alphabetically.

Ruby String Exercises: Sort a string's characters alphabetically

Ruby Code:

def check_string(str)
   return str.chars.sort.join
end
print check_string("javascript")
print "\n",check_string("python")
print "\n",check_string("PHP")

Output:

aacijprstv
hnopty
HPP

Flowchart:

Flowchart: Sort a string's characters alphabetically

Ruby Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Ruby program to count the occurrences of a specified character in a given string.
Next: Write a Ruby program to remove a specified character into a given string.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.