Data Structures & AlgorithmsHardGATE CS · DSA

Reverse the order of words, collapsing multiple spaces. What is printed?

Reverse the order of words, collapsing multiple spaces. What is printed?
s = "  the   is   new   code "
w = s.split()
print("|" + " ".join(reversed(w)) + "|", len(s.split(" ")))
  1. A. |code new is the| 4
  2. B. |code new is the| 12
  3. C. |code new is the| 13
  4. D. |code new is the| 14

Correct answer: C. |code new is the| 13

split() with no argument drops empty strings, but split(" ") keeps an empty string for every extra space. Output: |code new is the| 13.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic