Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

string

I have a question that 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 this line.

Write a method to replace all spaces in a string with '%20'. Assume string has sufficient space at end of string to hold additional characters, and that you're given a true length of a string. I used the books code, implementing the solution in Java 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);
}
like image 330
Cindy Avatar asked Oct 25 '14 02:10

Cindy


People also ask

What is the meaning of str i '\ 0?

if (str[i] != '\0') means "if not at end of the string" The last char in a C string (char array) is a null ( '\0' ) value. Follow this answer to receive notifications. answered Nov 26, 2020 at 8:34.

What is the significance of '\ 0 in character arrays?

\0 is not part of data in character string. It is indicator of end of string. If length of string is not known, look for this indicator.

What is the meaning of '\ 0?

'\0' is referred to as NULL character or NULL terminator It is the character equivalent of integer 0(zero) as it refers to nothing In C language it is generally used to mark an end of a string.

What is the meaning of '\ 0 in C?

\0 is zero character. In C it is mostly used to indicate the termination of a character string. Of course it is a regular character and may be used as such but this is rarely the case. The simpler versions of the built-in string manipulation functions in C require that your string is null-terminated(or ends with \0 ).


2 Answers

The standard library of the C programming language commonly uses NUL-terminated strings. A NUL-terminated string is stored in an array, and the string itself consists of all the characters in an array prior to the first '\0' character (called a NUL in ASCII). For example, if the elements of a character array are {'H','e','l','l','o','\0','i','d','1','0','t'}, the string is "Hello", with everything after the NUL ignored. If you write '\0' to position n of a NUL-terminated string, you will cut off everything after the first n characters, which reduces the string's length to no more than n. The writes to str[newLength - 1] and the like write characters just before the NUL.

I notice you're using the Java programming language, and you're probably trying to translate C code from your book literally to Java. Unlike C, Java doesn't usually use NUL-terminated strings, except in advanced things like JNI where Java code has to talk to C code. Also unlike C, you usually don't have to modify things in place but can instead freely create new copies, as Java has automatic garbage collection to get rid of objects and arrays that your code is no longer using.

Java more often uses the StringBuilder class to construct a string before making an immutable String out of it. So an idiomatic Java solution would create an empty StringBuilder, loop through the characters in an existing String, append("%20") for each character that is a space or append the same character otherwise, and finally convert the StringBuilder instance to a String.

like image 69
Damian Yerrick Avatar answered Oct 05 '22 23:10

Damian Yerrick


Java is not C nor C++; Java doesn't use the '\0' character to indicate the end of string ('end of char array' actually in C and C++). Java keeps a 'length' field for arrays so it can do bound checking behind the scenes.

like image 43
wittakarn Avatar answered Oct 06 '22 00:10

wittakarn