Move the Spaces to Front of the String

Problem: Given a string that has set of words and spaces, write a program to move the spaces to front of string, you need to traverse the array only once and need to adjust the string in place. string = “move these spaces to beginning” output =” movethesepacestobeginning” Solution: Maintain two indices i and j. Traverse from end to beginning. If the current index contains char, swap chars in index i with index j. [Read More]

Replace all spaces in a string with “%20″ in c-style string

Problem Write a C program that will replace all spaces with ‘%20′ Example Input: “Mr John Smith " Output: “Mr%20John%20Smith Solution The algorithm is as follows: Count the number of spaces during the first scan of the string. Parse the string again from the end and for each character: If a space is encountered, store “%20”. Else, store the character as it is in the newly shifted location. [Read More]