Replace Spaces

Posted: 22 Feb, 2021
Difficulty: Easy

PROBLEM STATEMENT

Try Problem

You have been given a string 'STR' of words. You need to replace all the spaces between words with “@40”.

Input Format:
The first line contains a single integer ‘T’ representing the number of test cases. 

The first line of each test case will contain a single string 'STR' consisting of one or more words. 
Output Format:
For each test case, return the modified string after replacing all the spaces between words with “@40”.

Print the output of each test case in a separate line.
Note:
You don’t need to print anything, It has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 50
0 <= |STR| <= 100

Where ‘|STR|’ is the length of a particular string including spaces.

Time limit: 1 sec
Approach 1

The basic idea is to use a separate string to store the resultant string. Copy characters from the original string to the resultant string one by one, and whenever there comes a space, simply add “@40” in the resultant string in place of the space.

 

The steps are as follows:

 

  • Initialise a string let’s say 'RES, to store the resultant string.
  • Start traversing through the string one by one and do the following:
    • If you come through space:
      • Add “@40” in place of space.
    • If it is any other character:
      • Simply add the character.
  • Return the resultant string ‘RES’.
Try Problem