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.

6
  • refer this stackoverflow.com/questions/3958955/… Commented Feb 23, 2016 at 4:10
  • Does this work with chars? And on top of that, arrays? It seems it only applies to strings Commented Feb 23, 2016 at 4:13
  • You want all the spaces to be moved to the end. right? Commented Feb 23, 2016 at 4:15
  • Correct, all spaces will go to the end as \u0000 characters Commented Feb 23, 2016 at 4:16
  • Did your problem get solved? Commented Feb 24, 2016 at 7:00

3 Answers 3

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

    }
Sign up to request clarification or add additional context in comments.

2 Comments

I deleted my answer because you answer is more helpful and simple way, so +1 uv
@PiyushGupta You shouldn't have deleted that. Should have made appropriate corrections. Thanks for the support!
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';
    }
}

Comments

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.