Remove extra spaces from the string (in place)

Ok, so I posted earlier about trying (without any pre-configured features) to remove extra spaces so that

"this is <insert many spaces!> a test" will return

"this is a test"

Remove spaces from a string, but not at the beginning and end

As it was homework, I asked for no complete solutions, and some kind people provided me with the following

"If you need to do this in place, then create two pointers. One points to the character to be read and one to the character to be copied. When you encounter extra space, then use the" write "pointer to point to the next non-spatial character. Copy the character to the read position indicated by the write character, then move the read and write pointers to the character after the character to be copied.

The problem is that now I want to completely break my computer into pieces, since I annoy it so much. I did not understand that at that time I could not use the char array, so I used array indexes to do this, I thought I could tell how to make it work, but now I just use pointers, and it is very difficult for me. I really need help, not complete solutions. So far this is what I do;

1) create a pointer called write, so I have no where to write to

2) create a pointer called read, so I no where to read from

(both of these pointers now point to the first element of the char array)

while (read != '\0')
    if read == a space 
        add one to read
        if read equals a space now 
            add one to write
            while read != a space {
                 set write to = read
            }
                 add one to both read and write
    else 
        add one to read and write 
        write = read  
+3
source share
7 answers

, , .

, - , , , - , .

+6

. , ; , , .

, . . , .

, , , . , .

+2
char p[] = "this is              a test";
char *readptr = &p[0];
char *writeptr = &p[0];

int inspaces = 0;
while(*readptr) {
 if(isspace(*readptr)) {
   inspaces ++;
 } else {
  inspaces = 0;
 }
 if(inspaces <= 1) {
  *writeptr = *readptr;
   writeptr++;
 }
 readptr++;
}
*writeptr = 0;
+1

, ( ) :

dm9pZCBTdHJpcFNwYWNlcyAoY2hhciAqdGV4dCkNCnsNCiAgY2hhciAqc3JjID0gdGV4dCwgKmRl
c3QgPSB0ZXh0Ow0KICB3aGlsZSAoKnNyYykNCiAgew0KICAgICpkZXN0ID0gKihzcmMrKyk7DQog
ICAgaWYgKCpzcmMgIT0gJyAnIHx8ICpkZXN0ICE9ICcgJykNCiAgICB7DQogICAgICArK2Rlc3Q7
DQogICAgfQ0KICB9DQogICpkZXN0ID0gMDsNCn0NCg==

Base64 , . , ( "" ).

+1

, ​​, :

void removeSpaces(char * str){

   /* ... stuff that changes the contents of str[] */

}

, . , , . , . (.. Read_pointer - write_pointer) , , . , (), , . ('\ 0'), .

0

? , . \s{2,}? . \s (, , ); {2,} 2 ; ? . ( : , , regex pro. , .net, C .)

0

- :

make a second char ** that has the same length as the first. When you run through the first array, your pointers keep an extra pointer in the last place you saw. If the last place you saw was previous, then you are not copying this char to the second array.

But I would start with something that goes through char ** with each character and prints each char. If you can do this, you can work on copying them into a second char **.

-1
source

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


All Articles