题解 | 编写函数实现字符串翻转(引用方式)

#include<bits/stdc++.h>
using namespace std;
// write your code here......
void fanzhuan(string& s){
    int len = s.size();
    //双指针
    int left = 0;
    int right = len - 1;
    char temp = ' ';
    while (left <= right) {
        temp = s[left];
        s[left] = s[right];
        s[right] = temp;
        left ++;
        right --;
    }
}

int main(){
    string s;
    getline(cin,s);
    // write your code here......
    fanzhuan(s);
    
    cout<<s;
    return 0;
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
04-23 10:48
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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