"Removing" Duplicating spaces from a char java array

I am trying to create a method that will take a char array, cut out any duplicate spaces (2 or more), and then put the characters '\u0000'at the end, however many spaces would be cut out so that the length of the array is satisfied. I understand that I need to push the elements down, but this is where I have problems. My program works fine with 2 spaces, but a sequence of three in a row will throw it out. I understand why this is happening, but I do not know how to fix it. I know this is related to the code characters[j] = characters[j+1], but I do not know how to fix it.

int duplicateCount = 0;
// Create a loop to read the element values
for(int i = 0; i + 1 < characters.length; i++){
    // If the element is a space and the next one is a space
    if(characters[i] == ' ' && characters[i+1] == ' '){
        // Add to duplicate count and start shifting values down
        duplicateCount++;
        // *THIS IS WHERE I THINK BUG IS*
        for(int j = i; j < characters.length - 1; j++){
            characters[j] = characters[j+1];
            }
        }
    }
    // Replace characters at end with how many duplicates were found
    for(int replace = characters.length - duplicateCount; replace < characters.length; replace++){
        characters[replace] = '\u0000';
    }
}

Thanks to everyone.

+4
source share
3 answers

, , , \u0000 .

, : loops if.

for (int i = 0; i < characters.length; i++) {
        int j =i+1;
        if (characters[i] == ' ' || characters[i] == '\u0000' ) {
            while (j<characters.length && (characters[j] == ' ' || characters[j] == '\u0000')) {

                j++;  //increment j till a non-space char is found

            }
            if(j<characters.length && (characters[j] != ' ' || characters[j] != '\u0000')) 
                // to ensure that the control entered while
            {
           characters[i] = characters[j];   //swapping the values
            characters[j] = '\u0000';    //giving value \u0000 to position j
        }
        }

    }
+1

, . 1 -.

int duplicateCount = 0;
// Create a loop to read the element values
for(int i = 0; i + 1 < characters.length; i++){
    // If the element is a space and the next one is a space
    if(characters[i] == ' ' && characters[i+1] == ' '){
        // Add to duplicate count and start shifting values down
        duplicateCount++;
        // *THIS IS WHERE I THINK BUG IS*
        for(int j = i; j < characters.length - 1; j++){
            characters[j] = characters[j+1];
            }
         i--; // so that multiple space case can be handled
        }
    }
    // Replace characters at end with how many duplicates were found
    for(int replace = characters.length - duplicateCount; replace < characters.length; replace++){
        characters[replace] = '\u0000';
    }
}
0
    int count = 0;  // Count of non-space elements

    // Traverse the array. If element encountered is
    // non-space, then replace the element at index 'count'
    // with this element
    for (int i = 0; i < n; i++)
        if (arr[i] != '')
            arr[count++] = arr[i]; // here count is
                                   // incremented

    // Now all non-space elements have been shifted to
    // Make all elements '\u0000' from count to end.
    while (count < n)
        arr[count++] = 0;
0
source

Source: https://habr.com/ru/post/1629993/


All Articles