# Replace all spaces in a string with “%20″

Problem: Write a C program that will replace all spaces with ‘%20′

Solution:
The algorithm is as follows:
1. Count the number of spaces during the first scan of the string.
2. 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.

```public static void ReplaceFun(char[] str, int length) {
int spaceCount = 0, newLength, i = 0;

for (i = 0; i < length; i++)
{
if (str[i] == ‘ ‘)
{
spaceCount++;
}
}

newLength = length + spaceCount * 2;
str[newLength] = ‘\0’;
for (i = length - 1; i >= 0; i--)
{
if (str[i] == ‘ ‘)
{
str[newLength - 1] = ‘0’;
str[newLength - 2] = ‘2’;
str[newLength - 3] = ‘%’;
newLength = newLength - 3;

}
else
{
str[newLength - 1] = str[i];
newLength = newLength - 1;
}
}
}

```

Time Complexity: O(N), N is length of the string.

### 0 Thoughts on “Replace all spaces in a string with “%20″”

1. /*We can do this way too*/

class ReplaceAllSpacesWithSpecifiedString
{

public static void main(String[] args)
{
System.out.println(replaceAllSpacesWithSpecifiedString(“aa bb ccc dddd ee ff ggg “, “%20″));
}

public static String replaceAllSpacesWithSpecifiedString(String str, String replace)
{
StringBuilder sb = new StringBuilder();
int count = 0;

for(int i = 0; i < str.length(); i++)
{
if(str.charAt(i) != ' ')
count++;
}

for(int i = 0; ; i++)
{
if(str.charAt(i) != ' ')
{
sb.append(str.charAt(i));
count–;
}
else
sb.append("%20");
if (count == 0)
break;
}
return sb.toString();
}

2. Ashish Singh on November 22, 2014 at 1:01 pm said:

str[newLength] = ‘’; is wrong..It will produce indexOutOfBoundException as Size of char Array is length of String.This is not dynamic allocation..

3. Ashish Singh on November 22, 2014 at 1:01 pm said:

str[newLength] = ‘’; is wrong..It will produce indexOutOfBoundException as Size of char Array is length of String.This is not dynamic allocation..

4. Shobhank on December 7, 2014 at 9:56 am said:

public static String spaceEncode(String str){
int i = 0;
while(i<str.length()){
if(str.charAt(i)==' '){
str = str.substring(0,i) + "%20" +str.substring(i+1, str.length());
i = i+3;
}else{
i++;
}
}
return str;
}

5. I tried with below code and works fine for me :

public void removeSpaces() {
System.out.println(“Enter the String “);
String s = new Scanner(System.in).nextLine();
// String s1 =”V I K K I ” ;

System.out.println(s.length());
String newStr =””;
for(int i =0; i< s.length() ;i++) {
char c = s.charAt(i);
if(c == ' ') {
// if the word has the space , concat the %20 to the new string
newStr = newStr+'%'+'2'+'0';
}
else {
// do nothing and proceed to next
newStr = newStr+s.charAt(i);

}
// newStr = newStr + s.charAt(i);

}
System.out.println(newStr);

}

6. Arun Kumar on May 14, 2015 at 11:41 pm said:

The code is for C language, as specified in the question. so it will not generate any exception but will work fine.

7. for java …
static String replaceSpace(String str)
{

str=str.replaceAll(” “, “%20″);
return str;
}

8. Anshdeep Singh on October 7, 2015 at 4:30 pm said:

Java Code
public static void replaceSpaces(char[] str,int length){
int spaceCount = 0,newLength=0,i=0;

//first scan
for (i=0;i<length;i++) {
if(str[i]==' '){
spaceCount++;
}
}
newLength = length + 2 * spaceCount;

char[] newArray = new char[newLength+1];
newArray[newLength] = '';
int newArrayPosition = 0;

//second scan
for (i=0;i<length;i++) {
if(str[i]==' '){
newArray[newArrayPosition] = '%';
newArray[newArrayPosition+1] = '2';
newArray[newArrayPosition+2] = '0';
newArrayPosition = newArrayPosition+3;
}

else{
newArray[newArrayPosition] = str[i];
newArrayPosition++;
}
}
System.out.println(newArray);
}