Interview Question : Trim multiple consecutive spaces from a string

前端 未结 11 825
情书的邮戳
情书的邮戳 2020-12-08 17:34

This is an interview question Looking for best optimal solution to trim multiple spaces from a string. This operation should be in-place operation.

input  =          


        
11条回答
  •  独厮守ぢ
    2020-12-08 17:35

    This is a very simple implementation of removing extra whitespaces.

    #include 
    std::string trimExtraWhiteSpaces(std::string &str);
    int main(){
        std::string str = "  Apple    is a     fruit  and I like     it   .  ";
        str = trimExtraWhiteSpaces(str);
        std::cout<

提交回复
热议问题