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?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/ruby-exercises/string/ruby-string-exercise-11.php