How to replace two characters with one character in a char array? Let me explain a bit more. I have a char array of length n . In this char array i want to replace two characters with one character in a specified index i. In this process the array length is going to decrease by 1.
The idea which i came to my mind is, first create a new char array of length n-1 then copy all elements from index 0 to index i (i excluding) then insert desired character at index i then copy elements from index i+2 (i including) to the index n-1. But this process require two times for loop. Is there any better approach which can do the same in efficient manner.
How to replace two characters with one character in a char array? Let me
Share
Or a more efficient way of doing this is to use a StringBuilder which is a wrapper for char[] and let it do it for you.
prints
You can look at the code for replace to see how it does it.