Code Lab

Modo entrevista. Cronômetro de 10 min, sem dicas e sem autocompletar

Reverse one word in a sentence

MédiaStrings10 min

Write a function reverseWord(sentence, word) that returns the string sentence where every word equal to word is written backwards. All other words stay as they are.

  • sentence has words separated by single spaces, with no spaces at the start or the end and no punctuation. It can be empty.
  • word is one word: at least one letter, no spaces.
  • Only a whole word that is exactly equal to word changes. Case matters: 'amazon' is not 'Amazon'.
  • A longer word that has word as a substring does not change: 'Amazonian' stays 'Amazonian'.
  • Every match is reversed, not only the first one.
  • If there is no match, return sentence unchanged. An empty sentence gives ''.

Tip: split the sentence into words, check each word, then join the words back.

Exemplos

reverseWord('I am working for Amazon', 'Amazon')

Retorna 'I am working for nozamA'

reverseWord('Amazon sells Amazonian books', 'Amazon')

Retorna 'nozamA sells Amazonian books'

Cobrada em Amazon · Fonte

Carregando o editor...
  1. EntradareverseWord('I am working for Amazon', 'Amazon')
    Esperado'I am working for nozamA'
  2. EntradareverseWord('I am working for Google', 'Amazon')
    Esperado'I am working for Google'

Os testes ocultos rodam no envio