-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"
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
Saurabh
  • 43
  • 1
  • 15

1 Answers1

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.

Jeffrey Cash
  • 1,023
  • 6
  • 12