What does str [newLength] = '\ 0 mean?

My question is, what does str [newLength] = '\ 0 mean? because I think the last character should be str [newLength-1], so I don't know the meaning of that string.

Write a method to replace all spaces in a string with "% 20". Suppose the string has enough space at the end of the string to store additional characters and that you are given the true length of the string. I used the book's code, implementing a Java solution using a character array (given the fact that Java strings are immutable):

public class Test {
public void replaseSpaces(char[] str, int length) {
    int spaceCount = 0, newLength = 0, 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;
        }
    }
    System.out.println(str);
}

      

+3


source to share


2 answers


The C standard library usually uses null terminated strings . A null-terminated string is stored in the array, and the string itself consists of all characters in the array up to the first character '\0'

(called NUL in ASCII). For example, if the elements are a character array {'H','e','l','l','o','\0','i','d','1','0','t'}

, string "Hello"

, with everything after ignoring NUL. If you write '\0'

at position n of a NUL-terminated string, you strip everything after the first n characters, which will reduce the length of the string to n. Recording in str[newLength - 1]

, etc. writes characters immediately before NUL.

I notice that you are using the Java programming language and you are probably trying to translate the C code from your book literally into Java. Unlike C, Java does not usually use null terminated strings, except for advanced things like JNI where Java code has to talk to C code. Also, unlike C, you usually don't need to change any things but they are free to create new copies instead, since Java has automatic garbage collection to get rid of objects and arrays that your code no longer uses.



Java most often uses a StringBuilder

class
to create a string before making it immutable from it String

. So an idiomatic Java solution would create an empty loop StringBuilder

, through the characters in the existing one String

, append("%20")

for every character that is a space or append

the same character otherwise, and finally convert the instance StringBuilder

to String

.

+13


source


Java is not C or C ++; Java does not use the "\ 0" character to indicate the end of a string ("end of char array" is actually in C and C ++). Java keeps a "length" field for arrays so it can bind behind.



+3


source







All Articles