How to remove spaces that are in a string sentence

后端 未结 5 1854
-上瘾入骨i
-上瘾入骨i 2021-01-25 13:48

I am trying to write a code to remove all the leading, trailing and middle of the sentence spaces but to keep only one space between the words.

For example if the input

5条回答
  •  野性不改
    2021-01-25 14:33

    Use two pointers. The first points at the next character to read, and the second points at the location to write.

    If you read two spaces in a row, don't advance the write pointer.


    Another approach you may want to consider is to use strtok with " " as a delimiter.

提交回复
热议问题