题解 | 多组_带空格的字符串_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, "");
    }

全部评论

相关推荐

06-12 16:00
天津大学 Java
牛客30236098...:腾讯坏事做尽,终面挂是最破防的 上次被挂了后我连简历都不刷了
点赞 评论 收藏
分享
allin秋招的单身...:我投过这家 上来就发个设计图给我,让我做好发到他邮箱
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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