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
A simple solution is to create an auxiliary string and copy characters one by one. Whenever space is encountered, place %20 in place of it.
Approach 1: using string.replace() function :
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void replaceSpaces(string input)
{
string rep = "%20" ;
for ( int i=0 ; i<input.length() ; i++)
{
if (input[i] == ' ' )
input.replace(i,1,rep);
}
cout<<input;
}
int main()
{
string input = "Mr John Smith" ;
replaceSpaces(input);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void replaceSpaces(String input)
{
String rep = "%20" ;
for ( int i = 0 ; i < input.length(); i++) {
if (input.charAt(i) == ' ' ) {
StringBuilder string
= new StringBuilder(input);
string.setCharAt(i, rep);
}
}
System.out.println(input);
}
public static void main(String[] args)
{
System.out.println( "GFG!" );
String input = "Mr John Smith" ;
replaceSpaces(input);
}
}
|
Python3
def replaceSpaces( input ):
rep = "%20"
for i in range ( len ( input )):
if ( input [i] = = ' ' ):
input = input .replace( input [i],rep)
print ( input )
input = "Mr John Smith"
replaceSpaces( input )
|
Javascript
<script>
function replaceSpaces(input)
{
let rep = "%20"
for (let i=0 ; i<input.length ; i++)
{
if (input[i] == ' ' )
input = input.replace(input[i],rep);
}
document.write(input);
}
let input = "Mr John Smith"
replaceSpaces(input)
</script>
|
C#
using System;
class GFG
{
public static void Main()
{
String str = "Mr John Smith " ;
str = str.Trim();
for ( int i = 0; i < str.Length; i++)
if (str[i] == ' ' )
str = str.Replace( " " , "%20" );
Console.Write(str);
}
}
|
Time complexity: O(N2) where N is the length of the string. because it is using replace method inside for loop
Auxiliary space: O(1).
Approach 2: A better solution to do in-place assuming that we have extra space in the input string. We first count the number of spaces in the input string. Using this count, we can find the length of the modified (or result) string. After computing the new length we fill the string in-place from the end.
Below is the implementation of the above approach:
C++
#include<stdio.h>
const int MAX = 1000;
int replaceSpaces( char str[])
{
int space_count = 0, i;
for (i = 0; str[i]; i++)
if (str[i] == ' ' )
space_count++;
while (str[i-1] == ' ' )
{
space_count--;
i--;
}
int new_length = i + space_count * 2 + 1;
if (new_length > MAX)
return -1;
int index = new_length - 1;
str[index--] = '\0' ;
for ( int j=i-1; j>=0; j--)
{
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;
}
int main()
{
char str[MAX] = "Mr John Smith " ;
int new_length = replaceSpaces(str);
for ( int i=0; i<new_length; i++)
printf ( "%c" , str[i]);
return 0;
}
|
Java
class GFG
{
static int MAX = 1000 ;
static char [] replaceSpaces( char [] str)
{
int space_count = 0 , i = 0 ;
for (i = 0 ; i < str.length; i++)
if (str[i] == ' ' )
space_count++;
while (str[i - 1 ] == ' ' )
{
space_count--;
i--;
}
int new_length = i + space_count * 2 ;
if (new_length > MAX)
return str;
int index = new_length - 1 ;
char [] old_str = str;
str = new char [new_length];
for ( int j = i - 1 ; j >= 0 ; j--)
{
if (old_str[j] == ' ' )
{
str[index] = '0' ;
str[index - 1 ] = '2' ;
str[index - 2 ] = '%' ;
index = index - 3 ;
}
else
{
str[index] = old_str[j];
index--;
}
}
return str;
}
public static void main(String[] args)
{
char [] str = "Mr John Smith " .toCharArray();
str = replaceSpaces(str);
for ( int i = 0 ; i < str.length; i++)
System.out.print(str[i]);
}
}
|
Python3
MAX = 1000 ;
def replaceSpaces(string):
string = string.strip()
i = len (string)
space_count = string.count( ' ' )
new_length = i + space_count * 2
if new_length > MAX :
return - 1
index = new_length - 1
string = list (string)
for f in range (i - 2 , new_length - 2 ):
string.append( '0' )
for j in range (i - 1 , 0 , - 1 ):
if string[j] = = ' ' :
string[index] = '0'
string[index - 1 ] = '2'
string[index - 2 ] = '%'
index = index - 3
else :
string[index] = string[j]
index - = 1
return ''.join(string)
if __name__ = = '__main__' :
s = "Mr John Smith "
s = replaceSpaces(s)
print (s)
|
C#
using System;
class GFG
{
static int MAX = 1000;
static char [] replaceSpaces( char [] str)
{
int space_count = 0, i = 0;
for (i = 0; i < str.Length; i++)
if (str[i] == ' ' )
space_count++;
while (str[i - 1] == ' ' )
{
space_count--;
i--;
}
int new_length = i + space_count * 2;
if (new_length > MAX)
return str;
int index = new_length - 1;
char [] new_str = str;
str = new char [new_length];
for ( int j = i - 1; j >= 0; j--)
{
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;
}
public static void Main(String[] args)
{
char [] str = "Mr John Smith " .ToCharArray();
str = replaceSpaces(str);
for ( int i = 0; i < str.Length; i++)
Console.Write(str[i]);
}
}
|
Javascript
const MAX = 1000;
function replaceSpaces( str)
{
let space_count = 0, i;
for (i = 0; str[i]; i++)
if (str[i] == ' ' )
space_count++;
while (str[i-1] == ' ' )
{
space_count--;
i--;
}
let new_length = i + space_count * 2 + 1;
if (new_length > MAX)
return -1;
let index = new_length - 1;
str[index--] = '\0' ;
let new_str= "" ;
for (let j=i-1; j>=0; j--)
{
if (str[j] == ' ' )
{
new_str= "%20" +new_str;
}
else
{
new_str=str[j]+new_str;
}
}
console.log(new_str);
return new_length;
}
let str = "Mr John Smith " ;
let new_length = replaceSpaces(str);
|
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 3: Trim the string and call replaceAll() method, to replace all space Unicode to %20.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "Mr John Smith " ;
str.erase(str.find_last_not_of( " \n\r\t" )+1);
size_t pos = str.find( " " );
while (pos != string::npos)
{
str.replace(pos, 1, "%20" );
pos = str.find( " " , pos + 3);
}
cout << str << endl;
return 0;
}
|
Java
class GFG
{
public static void main(String[] args)
{
String str = "Mr John Smith " ;
str = str.trim();
str = str.replaceAll( "\\s" , "%20" );
System.out.println(str);
}
}
|
Python3
s = "Mr John Smith "
s = s.strip()
s = s.replace( ' ' , "%20" )
print (s)
|
C#
using System;
class GFG
{
public static void Main()
{
String str = "Mr John Smith " ;
str = str.Trim();
str = str.Replace( " " , "%20" );
Console.Write(str);
}
}
|
Javascript
<script>
var str = "Mr John Smith " ;
/ we can use regular expression for checking empty space /
str = str.replace(/ /g, "%20" )
document.write(str);
</script>
|
Time complexity: O(N) where N is the length of the string.
Auxiliary space: O(1).
This article is contributed by Aarti_Rathi and Brahmani Sai. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.