-1

I want to remove spaces from an array and rearrange the values of the array.

array[]="1  2 6    9  5";

I want the values of array like

array[]="12695"
3
  • 3
    Nulls and spaces are not the same thing. Which do you actually want to remove? Commented Mar 9, 2019 at 5:05
  • 1
    Seems like you want to remove spaces in the string.check this Commented Mar 9, 2019 at 5:18
  • 1
    Sorry, I didn't know that they are different. If they are different then i want to remove spaces. Commented Mar 9, 2019 at 5:20

1 Answer 1

3

There are tons of ways to whip up a simple solution to remove spaces from a string. Here's a little example I came up with using pointer iteration:

void remove_spaces(char * str){
    char * back = str;
    do{
        if(*back != ' ')
            *str++ = *back;
    }while(*back++ != '\0');
}

This code uses two pointers to iterate across the given string. At each step of the loop, the back pointer is checked against the null character (to see if the end of the string has been reached) and incremented. In the body of the loop, the value from the back pointer is copied into the front pointer whenever back is not pointing to a ' '. str is also incremented right after this copy from *back to *str is made, using the post-increment (++) operator.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.