Swap Word Pairs

String Manipulation Arrays Basic Algorithms

Problem Description

Given a sentence containing a sequence of words separated by spaces, write a function that swaps every two adjacent words. If the number of words in the sentence is odd, leave the last word in its original position.

Example

  • Input: "Hello world this is a test"
  • Output: "world Hello is this test a"

Notes

  • Words are separated by single spaces.
  • Preserve the order as per the rule: swap the first two words, then swap the next two, and so on.

Hint: Think about how you might split the string into words, process the list of words, and join them back together.

Happy coding on this fine January day!