题解 | 多组_带空格的字符串_T组形式

多组_带空格的字符串_T组形式

https://www.nowcoder.com/practice/cff28a28d7f54419a640a8bb19f4275f

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main() {
    int t, n;
    size_t bstart {}, bend {};
    string s;
    cin >> t;
    while(t--) {
        cin >> n;
        cin.ignore(); // 忽略前一个输入后的换行符
        getline(cin, s);
        while (true) {
            bstart = s.find(' ');
            if (bstart == string::npos) break;
            bend = s.find(' ', bstart);
            s.replace(bstart, bend-bstart+1, "");
        }

        
        reverse(s.begin(), s.end());
        cout << s << endl;
    }
}

去除字符串空格的代码如下:

    while (true) {
        bstart = s.find(' ');
        if (bstart == string::npos) break;
        bend = s.find(' ', bstart);
        s.replace(bstart, bend-bstart+1, "");
    }

全部评论

相关推荐

LZStarV:冲就好了,就算真的是字节也冲,面评脏了大不了等三四个月就淡了,而且等到那个时候实力进步了选择还多,何必拘泥于字节
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务