1

I am preparing for an interview and I have been looking at practice coding problems and I had a few questions about the solutions for the code.

The problem was: write a method to replace all spaces in a string with '%20'. You can assume that the string has enough space at the end to hold extra characters and that you are given the real length of the string. Example:

input: "My dog ", 6

output: "My%20dog"

Solution:

    void replaceSpaces(char[]str, int length){
     int spaceCount = 0, newLength, i;
     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;
        }
       }
      }

First question with this code are how would I implement this in the main class? I wanted to get a better idea of how exactly the char array works and see if I can test this code.

Secondly, what does this line mean and what is its purpose? I tried to look up what '/0' means in java but could find nothing:

       str[newLength] = '/0';

Thirdly why do we need to subtract 3 from the newLength in the second half of the code where we are adding in space for the %20? This is the line below:

       newLength = newLength - 3;
4
  • 2
    Why don't you use a String rather than char[]? Commented Oct 15, 2015 at 15:00
  • 2
    This looks like code that is trying to be copied from C/C++, especially given the fact that a bad null-terminator is being used (it's '\0', but it has no place in Java code). Commented Oct 15, 2015 at 15:18
  • String newString = new String(str).replaceAll(" ", "%20"); ? Commented Oct 15, 2015 at 15:20
  • This is not my solution but a solution from a book that prepares new grads for interviews. In the solution it is implied that this is java code because they state that they had to use character arrays because strings in Java are immutable. But 3kings your solution is the solution I would have used, but I guess the purpose is to show your programming skills. Commented Oct 15, 2015 at 15:42

3 Answers 3

3

First question with this code are how would I implement this in the main class? I wanted to get a better idea of how exactly the char array works and see if I can test this code.

Answer: You have two choices. 1. Use tools like eclipse. It gives you good UI which serves easily making a class containing main() function and debugging tool for your observation. 2.Type the target characters in console by using System.out.println(str[i]);

Secondly, what does this line mean and what is its purpose? I tried to look up what '/0' means in java but could find nothing:

Answer: /0 is NULL which is the mark of end of the string in the char Array. It is about structure in memory & storage sides. For example there is dummy memory like this "safjlasjlkasjallsjalsaf" If code saves your one string like "APPLE", it is saved in some part like this. "safjlasjlAPPLEllsjalsaf" with start index. (ex. 10). It means that the variable don't know your string is whether APPLE or APPLEllsja.. So \0 is marked at the end of your string like this "safjlasjlAPPLE\0lsjalsaf"

\0 is also one character and bits are 000000000.

Thirdly why do we need to subtract 3 from the newLength in the second half of the code where we are adding in space for the %20? This is the line below:

Answer: The line is in order to point next index for next for loop. Since i and newLength are pointer that indicate target index (i is source string, newLength is dest string), newLength should be decreased 1 every loop like i (newLength = newLength - 1;). Because 3 characters (%20) are stored in dest string sequencely, newLength should be decreased 3 instead of 1)

Sign up to request clarification or add additional context in comments.

Comments

0

There is a syntax error firstly in the solution int spaceCount = 0; newLength, i; it's not a statement Now let's get to your questions

  1. You got the class with main method you just copy your solution method to the class and probably make it static. Use System.out.println() to print on the console the value of the variable you are interested. And try to fix the initial errors
  2. '\0' means null character it is used to identify end of the string
  3. 3 is subtracted because 3 characters are added to the str

P.S in comments there is a great solution for this problem try to stick with it and not this one :) Good luck with the interview

Comments

0

This code looks like it has been translated from C or C++, where \0 means NULL. The best way for you to really understand that algo, is to test and debug it.

You could try it that way:

public static void main(String[] args){
     char array[] = new char[10]; 
     array[10] = {'M','y',' ','d','o','g'};
     replaceSpaces(array, array.length);
}



static void replaceSpaces(char[]str, int length){
    int spaceCount = 0; newLength, i;
    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;
        }
    }
}

and add some System.out.println();

1 Comment

And '\0' for a NUL char.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.