# URLify a given string (Replace spaces is %20)

Write a method to replace all the spaces in a string with ‘%20’. You may assume that the string has sufficient space at the end to hold the additional characters, and that you are given the “true” length of the string.

Examples:

```Input : "Mr John Smith", 13
Output : Mr%20John%20Smith

Input : "Mr John Smith   ", 13
Output : Mr%20John%20Smith
```

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

A simple solution is to create an auxiliary string and copy characters one by one. Whenever a space is encountered, place %20 in place of it.

A better solution to do in-place assuming that we have extra space in input string. We fist count number of spaces in input string. Using this count, we can find length of the modified (or result) string. After computing new length we fill the string in-place from end.

## C++

 `// C++ program to replace spaces with %20 ` `#include ` ` `  `// Maximum length of string after modifications. ` `const` `int` `MAX = 1000; ` ` `  `// Replaces spaces with %20 in-place and returns ` `// new length of modified string. It returns -1 ` `// if modified string cannot be stored in str[] ` `int` `replaceSpaces(``char` `str[]) ` `{ ` `    ``// count spaces and find current length ` `    ``int` `space_count = 0, i; ` `    ``for` `(i = 0; str[i]; i++) ` `        ``if` `(str[i] == ``' '``) ` `            ``space_count++; ` ` `  `    ``// Remove trailing spaces ` `    ``while` `(str[i-1] == ``' '``) ` `    ``{ ` `       ``space_count--; ` `       ``i--; ` `    ``} ` ` `  `    ``// Find new length. ` `    ``int` `new_length = i + space_count * 2 + 1; ` ` `  `    ``// New length must be smaller than length ` `    ``// of string provided. ` `    ``if` `(new_length > MAX) ` `        ``return` `-1; ` ` `  `    ``// Start filling character from end ` `    ``int` `index = new_length - 1; ` ` `  `    ``// Fill string termination. ` `    ``str[index--] = ``'\0'``; ` ` `  `    ``// Fill rest of the string from end ` `    ``for` `(``int` `j=i-1; j>=0; j--) ` `    ``{ ` `        ``// inserts %20 in place of space ` `        ``if` `(str[j] == ``' '``) ` `        ``{ ` `            ``str[index] = ``'0'``; ` `            ``str[index - 1] = ``'2'``; ` `            ``str[index - 2] = ``'%'``; ` `            ``index = index - 3; ` `        ``} ` `        ``else` `        ``{ ` `            ``str[index] = str[j]; ` `            ``index--; ` `        ``} ` `    ``} ` ` `  `    ``return` `new_length; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``char` `str[MAX] = ``"Mr John Smith   "``; ` ` `  `    ``// Prints the replaced string ` `    ``int` `new_length = replaceSpaces(str); ` `    ``for` `(``int` `i=0; i

## Java

 `// Java program to replace spaces with %20  ` `class` `GFG ` `{ ` ` `  `    ``// Maximum length of string ` `    ``// after modifications. ` `    ``static` `int` `MAX = ``1000``; ` ` `  `    ``// Replaces spaces with %20 in-place ` `    ``// and returns new length of modified string. ` `    ``// It returns -1 if modified string ` `    ``// cannot be stored in str[] ` `    ``static` `char``[] replaceSpaces(``char``[] str) ` `    ``{ ` ` `  `        ``// count spaces and find current length ` `        ``int` `space_count = ``0``, i = ``0``; ` `        ``for` `(i = ``0``; i < str.length; i++) ` `            ``if` `(str[i] == ``' '``) ` `                ``space_count++; ` ` `  `        ``// count spaces and find current length ` `        ``while` `(str[i - ``1``] == ``' '``) ` `        ``{ ` `            ``space_count--; ` `            ``i--; ` `        ``} ` ` `  `        ``// Find new length. ` `        ``int` `new_length = i + space_count * ``2``; ` ` `  `        ``// New length must be smaller than length ` `        ``// of string provided. ` `        ``if` `(new_length > MAX) ` `            ``return` `str; ` ` `  `        ``// Start filling character from end ` `        ``int` `index = new_length - ``1``; ` ` `  `        ``char``[] new_str = str; ` `        ``str = ``new` `char``[new_length]; ` ` `  `        ``// Fill rest of the string from end ` `        ``for` `(``int` `j = i - ``1``; j >= ``0``; j--)  ` `        ``{ ` ` `  `            ``// inserts %20 in place of space ` `            ``if` `(new_str[j] == ``' '``)  ` `            ``{ ` `                ``str[index] = ``'0'``; ` `                ``str[index - ``1``] = ``'2'``; ` `                ``str[index - ``2``] = ``'%'``; ` `                ``index = index - ``3``; ` `            ``}  ` `             `  `            ``else`  `            ``{ ` `                ``str[index] = new_str[j]; ` `                ``index--; ` `            ``} ` `        ``} ` `        ``return` `str; ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `main(String[] args)  ` `    ``{ ` `        ``char``[] str = ``"Mr John Smith "``.toCharArray(); ` ` `  `        ``// Prints the replaced string ` `        ``str = replaceSpaces(str); ` ` `  `        ``for` `(``int` `i = ``0``; i < str.length; i++) ` `            ``System.out.print(str[i]); ` `    ``} ` `} ` ` `  `// This code is contributed by ` `// sanjeev2552 `

Output:

```Mr%20John%20Smith
```

Time Complexity : O(n) where n is the true length of the string.
Auxiliary Space : O(1) because the above program is an inplace algorithm.

Approach 2:
Trim the string and call replaceAll() method, to replace all space unicode to %20.

## Java

 `// Java implementation of above approach ` `class` `GFG ` `{ ` `    ``public` `static` `void` `main(String[] args)  ` `    ``{ ` `        ``// Instantiate the string ` `        ``String str = ``"Mr John Smith   "``; ` `         `  `        ``// Trim the given string ` `        ``str = str.trim(); ` `         `  `        ``// Replace All space (unicode is \\s) to %20 ` `        ``str = str.replaceAll(``"\\s"``, ``"%20"``); ` `         `  `        ``// Display the result ` `        ``System.out.println(str); ` `    ``} ` `} `

## C#

 `// C# implementation of above approach ` `using` `System; ` `     `  `class` `GFG ` `{ ` `    ``public` `static` `void` `Main()  ` `    ``{ ` `        ``// Instantiate the string ` `        ``String str = ``"Mr John Smith   "``; ` `          `  `        ``// Trim the given string ` `        ``str = str.Trim(); ` `          `  `        ``// Replace All space (unicode is \\s) to %20 ` `        ``str = str.Replace(``" "``, ``"%20"``); ` `          `  `        ``// Display the result ` `        ``Console.Write(str); ` `    ``} ` `} ` ` `  `// This code is contributed by Rajput-Ji  `

Output:

```Mr%20John%20Smith
```

This article is contributed by Brahmani Sai. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.