Remove extra white spaces in C++

前端 未结 12 1993
日久生厌
日久生厌 2021-02-05 12:01

I tried to write a script that removes extra white spaces but I didn\'t manage to finish it.

Basically I want to transform abc sssd g g sdg gg gf into

12条回答
  •  没有蜡笔的小新
    2021-02-05 12:20

    Well here is a longish(but easy) solution that does not use pointers. It can be optimized further but hey it works.

    #include 
    #include 
    using namespace std;
    void removeExtraSpace(string str);
    int main(){
        string s;
        cout << "Enter a string with extra spaces: ";
        getline(cin, s);
        removeExtraSpace(s);
        return 0;
    }
    void removeExtraSpace(string str){
        int len = str.size();
        if(len==0){
            cout << "Simplified String: " << endl;
            cout << "I would appreciate it if you could enter more than 0 characters. " << endl;
            return;
        }
        char ch1[len];
        char ch2[len];
        //Placing characters of str in ch1[]
        for(int i=0; i

提交回复
热议问题