Handle Score
@Ibrahim Nash 6420
@blackshadows 6380
@mb1973 5704
@Quandray 5245
@akhayrutdinov 5111
@saiujwal13083 5046
@sanjay05 3762
@kirtidee18 3673
@marius_valentin_dragoi 3523
@mantu_singh 3510
@sushant_a 3459
Complete Leaderboard
Reverse a string with spaces intact
Basic Accuracy: 42.11% Submissions: 844 Points: 1

Given a string, your task is to reverse the string keeping the spaces intact to their positions.

Example 1:

S = "Help others"
Output: sreh topleH
Explanation: The space is intact at index 4
while all other characters are reversed.

​Example 2:

S = "geeksforgeeks"
Output: skeegrofskeeg
Explanation: No space present, hence the
entire string is reversed.

Your Task:
You don't need to read input or print anything. Your task is to complete the function reverseWithSpacesIntact() which takes the string S as input and returns the resultant string by reversing the string while keeping all the spaces intact.

Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(1).



to report an issue on this page.


We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?


All Submissions

My Submissions:

Login to access your submissions.

Reverse a string with spaces intact

Output Window