数据结构-字符串I

字符串-反转字符串

题目链接

原理如下:

//字符串-反转字符串
#include<bits/stdc++.h>
using namespace std;
string ch;
int main(){
    cin >> ch;
    for (int i = 0, j = ch.size()-1; i < j; i++, j--){
        swap(ch[i], ch[j]);
    }
    cout << ch << '\n';
    return 0;
}

字符串-反转字符串II

题目链接

//字符串-反转字符串II
#include<bits/stdc++.h>
using namespace std;
string s;
int k;
int main(){
    cin >> s >> k;
    for (int i = 0; i < s.size(); i += 2 * k){
        if (i + k <= s.size()){
            reverse(s.begin() + i, s.begin() + i + k);
        }
        else {
            reverse(s.begin() + i, s.end());
        }
    }
    cout << s << '\n';
    return 0;
}

字符串-替换空格

题目链接

//字符串-替换空格
#include<bits/stdc++.h>
using namespace std;
string s;
int main(){
    getline(cin, s);
    int count = 0;
    int oldSize = s.size();
    for (int i = 0; i < s.size(); i++){
        if (s[i] == ' '){
            count++;
        }
    }
    s.resize(s.size() + count * 2);
    int newSize = s.size();
    for (int i = oldSize - 1, j = newSize - 1; i < j; i--, j--){
        if (s[i] == ' '){
            s[j] = '0', s[j - 1] = '2', s[j - 2] = '%';
            j -= 2;
        }
        else {
            s[j] = s[i];
        }
    }
    cout << s << '\n';
    return 0;
}

字符串-左旋转字符串

题目链接

//字符串-左旋转字符串
#include<bits/stdc++.h>
using namespace std;
string s;
int k;
int main(){
    getline(cin, s);
    cin >> k;
    reverse(s.begin(), s.begin() + k);
    reverse(s.begin() + k, s.end());
    reverse(s.begin(), s.end());
    cout << s << '\n';
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值