How to remove spaces that are in a string sentence

后端 未结 5 1857
-上瘾入骨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:28

    You could use a string tokenizer in the first instance, and use as a delimiter a whitespace(" "). For that purpose in C you use strtok.After that you parse the output of the strtok function, and for each element, if it contains more whitespaces character, you eliminate them. Also you put each corrected element in another array. Then, all you have to do is parse the new array and add a whitespace after every element. If you fancy this idea, I can provide a code draft for you.

提交回复
热议问题