v9.1.1
Menu

Word Reverser

easyFREEText ProcessingNLP 1: Foundations

Given a sentence, reverse each word individually while keeping the word order the same.

Example:

  • Input: "hello world"
  • Reversed words: "olleh dlrow"

Words are separated by single spaces. Preserve the original casing of each character.

Example

Input
hello world
Output
olleh dlrow
Reasoning
  • Input: text is one sentence, here hello world: two words, each a sequence of characters, with a single space between them.
  • One word: a word w=c1c2…cnw = c_1 c_2 \dots c_n becomes cncn−1…c1c_n c_{n-1} \dots c_1, the same letters read from last to first. For hello (n=5n = 5): c5=c_5 = o, c4=c_4 = l, c3=c_3 = l, c2=c_2 = e, c1=c_1 = h, giving olleh.
  • Second word: world read backwards is d, l, r, o, w, giving dlrow.
  • Output: the reversed words stay in their original positions with one space between them, so the result is olleh dlrow. Each word keeps its length, so the output has exactly as many characters as the input.
  • Notice: only the letters inside each word move, never the words themselves. Reversing the whole sentence would give dlrow olleh instead. Casing travels with each character, so NLP becomes PLN, and a one-letter word like a is unchanged.

Constraints

  • Input: A single line of text
  • Words are separated by single spaces
  • Output: Each word reversed, space-separated
  • Preserve original character casing

More from NLP 1: Foundations

solution.py

Test results

Sign in to run your code against the test cases.