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.
/*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();
}
str[newLength] = ‘ ’; is wrong..It will produce indexOutOfBoundException as Size of char Array is length of String.This is not dynamic allocation..
str[newLength] = ‘ ’; is wrong..It will produce indexOutOfBoundException as Size of char Array is length of String.This is not dynamic allocation..
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;
}
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);
}
The code is for C language, as specified in the question. so it will not generate any exception but will work fine.
for java …
static String replaceSpace(String str)
{
str=str.replaceAll(” “, “%20″);
return str;
}
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);
}