4

I'm attempting to create a method which will take a char array, cut out any duplicate spaces (2 or more) and then place '\u0000' characters at the end for however many spaces were cut out so that the array length is satisfied. I realize I have to shift the elements down but this is where I'm having trouble. My program works fine with 2 spaces but a sequence of three in a row will throw it off. I understand why this is happening but I don't know how to fix it. I know it stems from the code characters[j] = characters[j+1] but I don't know how to go about fixing 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';
    }
}

Thank you all.

Piyush Gupta
  • 2,181
  • 3
  • 13
  • 28
dj1121
  • 439
  • 1
  • 7
  • 16

3 Answers3

1

From what I understood, you want all the spaces to be removed from between non-space characters and add \u0000 to the end.

If that's the issue, try this out: I have used loops and if statements to achieve it.

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
        }
        }

    }
Mathews Mathai
  • 1,707
  • 13
  • 31
0

Very simple solution if you want to keep your current code. Just add 1 line i--.

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';
    }
}
sanky jain
  • 873
  • 1
  • 6
  • 14
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;
bngk
  • 59
  • 14