Reverse Words In A String Problem Statement

Given a string of length N, reverse the string word by word. Ensure that the output reversed string has a single space between two words and does not contain leading or trailing spaces.

Example:

Input:
"  Welcome to   Coding  Ninjas"
Output:
"Ninjas Coding to Welcome"
Explanation:

The reversed string contains only a single space between words and eliminates any leading or trailing spaces.

Input:

The first line of input contains an integer T, representing the number of test cases. Each of the following T lines contains a string to be reversed word by word.

Output:

For each test case, return the reversed string with a single space between words and no trailing spaces.

Constraints:

  • 1 <= T <= 50
  • 0 <= N <= 10^5

Time Limit: 1 second

Be the first one to answer
Add answer anonymously...
Bajaj Finserv Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter