I was solving an easy question :
Remove certain characters of a character array in Java , the idea is straightforward :
static void remove_char(char[] arr, char c){
int r = 0;
for (int i = 0 ; i < arr.length ; i++){
if (arr[i] == c){
r++;
continue;
}
arr[i - r] = arr[i];
}
arr[arr.length - r] = '\0'; // ??
return;
}
I want to put an ending character which signals that the rest of the array doesn't have to be considered when we want to, for example, generate a string using new String(arr)
Is there any such character in Java ? ( I guess in C it's \0 but I am not sure)
For example when we call :
System.out.println(new String(remove_char(new char[] {'s','a','l','a','m'} , 'a')))
this will be printed : slm m
While I want to get slm and I want to do this in-place not using a new array
in-place)