Code Lab

Mode entretien. Minuteur de 10 min, sans indices ni autocomplétion

Remove the spaces and sort

MoyenStrings10 min

Write a function sortCharacters(text) that removes all whitespace from the string text and returns the other characters sorted.

  • text has only ASCII characters: letters, digits, punctuation and whitespace.
  • Whitespace is a space, a tab \t and a line break \n or \r. All of it goes, not only spaces.
  • Each character is sorted by its character code, from small to large. This is the order of sort without a compare function. For example, ! and , come before the digits, the digits come before the capital letters, and the capital letters come before the small letters. So 'Z' comes before 'a'.
  • Repeated characters stay: 'banana' gives 'aaabnn'.
  • Return a string. An empty string or a string of only whitespace gives ''.

Exemples

sortCharacters('My Name is Nikhil')

Renvoie 'MNNaehiiiklmsy'

sortCharacters('Zebra 123')

Renvoie '123Zaber'

Posé chez Cognizant · Source

Chargement de l'éditeur...
  1. EntréesortCharacters('My Name is Nikhil')
    Attendu'MNNaehiiiklmsy'
  2. EntréesortCharacters('b a')
    Attendu'ab'

Les tests cachés se lancent à la soumission