Write a program to Reverse the given string while preserving the position of spaces.
Input : "abc de" Output : edc ba Input : "intern at geeks" Output : skeegt an retni Input : "Help others" Output : sreh topleH
- Create a string to store result. Mark the space position of the given string in this string.
- Insert the character from input string into the result string in reverse order.
- while inserting the character check if the result string already contains a space at index ‘j’ or not. If it contains, we copy the character to the next position.
Below is the implementation of above steps.
Output: skeegrofsk ee gtapi hsn retni
skeegrofsk ee gtapi hsn retni
Time complexity -> O(N)
Space complexity -> O(N)
This article is contributed by Rishabh Jain. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Reverse individual words with O(1) extra space
- Find numbers of balancing positions in string
- Find time taken for signal to reach all positions in a string
- Number of positions where a letter can be inserted such that a string becomes palindrome
- Sort an alphanumeric string such that the positions of alphabets and numbers remain unchanged
- Create a new string by alternately combining the characters of two halves of the string in reverse
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Replace all occurrences of a string with space
- Program to replace every space in a string with hyphen
- Replace all occurrences of string AB with C without using extra space
- Remove duplicates from a string in O(1) extra space
- Different methods to reverse a string in C/C++
- Reverse the given string in the range [L, R]
- Reverse vowels in a given string
- Reverse words in a given string
Improved By : Sam007