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);
}
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.
\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.
'\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.
\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 ).
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With