Given a string, the task is to replace all the spaces between the words with a hyphen character ‘-‘.
Input: str = "Geeks for Geeks." Output: Geeks-for-Geeks. Input: str = "A computer science portal for geeks" Output: A-computer-science-portal-for-geeks
- Traverse the whole string character by character.
- If the character is a space, replace it with hyphen ‘-‘.
Below is the implementation of the above approach:
Time Complexity: O(N)
- Replace all occurrences of a string with space
- Replace all occurrences of string AB with C without using extra space
- Recursive program to replace all occurrences of pi with 3.14 in a given string
- Replace two substrings (of a string) with each other
- Replace a character c1 with c2 and c2 with c1 in a string S
- Replace all occurrences of pi with 3.14 in a given string
- URLify a given string (Replace spaces is %20)
- Replace all consonants with nearest vowels in a string
- Replace every consonant sequence with its length in the given string
- Program to replace a word with asterisks in a sentence
- Remove duplicates from a string in O(1) extra space
- Reverse a string preserving space positions
- Sum of all substrings of a string representing a number | Set 2 (Constant Extra Space)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.