Interview Question : Trim multiple consecutive spaces from a string

前端 未结 11 741
情书的邮戳
情书的邮戳 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

    Trimming multiple spaces also means a space should always be followed by a non space character.

    int pack = 0;
    char str[] = "I    Like    StackOverflow a      lot";
    for (int iter = 1; iter < strlen(str); iter++)
    {
        if (str[pack] == ' ' && str[iter] == ' ')
            continue;
        str[++pack] = str[iter];
    }
    str[++pack] = NULL;
    
    0 讨论(0)
  • 2020-12-08 17:35

    This is a very simple implementation of removing extra whitespaces.

    #include <iostream>
    std::string trimExtraWhiteSpaces(std::string &str);
    int main(){
        std::string str = "  Apple    is a     fruit  and I like     it   .  ";
        str = trimExtraWhiteSpaces(str);
        std::cout<<str;
    }
    std::string trimExtraWhiteSpaces(std::string &str){
        std::string s;
        bool first = true;
        bool space = false;
        std::string::iterator iter;
        for(iter = str.begin(); iter != str.end(); ++iter){
            if(*iter == ' '){
                if(first == false){
                    space = true;
                }
            }else{
                if(*iter != ',' && *iter != '.'){
                    if(space){
                        s.push_back(' ');
                    }
                }
                s.push_back(*iter);
                space = false;
                first = false;
            }
        }
        return s;
    }
    
    0 讨论(0)
  • 2020-12-08 17:36
    int j = 0;
    int k=0;
    char str[] = "I    Like    StackOverflow a      lot"; 
    int length = strlen(str);
    char str2[38];
    for (int i = 0; i < length; i++) 
    { 
        if (str[i] == ' ' && str[i+1] == ' ') 
         continue; 
        str2[j] = str[i];
        j++;
    } 
    
    str2[j] =NULL;
    
    cout<<str2;
    
    0 讨论(0)
  • 2020-12-08 17:42

    A c++0x - solution using a lambda instead of a regular function object. Compare to Cubbi's solution.

    #include <string>
    #include <algorithm>
    
    int main()
    {
        std::string str = "I    Like    StackOverflow a      lot";
    
        str.erase(std::unique(str.begin(), str.end(),
          [](char a, char b) { return a == ' ' && b == ' '; } ), str.end() );  
    }
    
    0 讨论(0)
  • 2020-12-08 17:44

    Does using <algorithm> qualify as "algorithmic solution"?

    #include <iostream>
    #include <string>
    #include <algorithm>
    #include <iterator>
    struct BothAre
    {
        char c;
        BothAre(char r) : c(r) {}
        bool operator()(char l, char r) const
        {
                return r == c && l == c;
        }
    };
    int main()
    {
        std::string str = "I    Like    StackOverflow a      lot";
        std::string::iterator i = unique(str.begin(), str.end(), BothAre(' '));
        std::copy(str.begin(), i, std::ostream_iterator<char>(std::cout, ""));
        std::cout << '\n';
    }
    

    test run: https://ideone.com/ITqxB

    0 讨论(0)
  • 2020-12-08 17:46

    I'd just go with this:

    int main(int argc, char* argv[])
    {
        char *f, *b, arr[] = "  This        is    a test.                ";
        f = b = arr;
    
        if (f) do
        {
            while(*f == ' ' && *(f+1) == ' ') f++;
        } while (*b++ = *f++);
    
        printf("%s", arr);
    
        return 0;
    }
    
    0 讨论(0)
提交回复
热议问题