字符串大小写移动位置

来源:互联网 发布:奥拉朱旺数据 编辑:程序博客网 时间:2024/05/17 01:44

腾讯 实习生题目

小Q最近遇到了一个难题:把一个字符串的大写字母放到字符串的后面,各个字符的相对位置不变,且不能申请额外的空间。
你能帮帮小Q吗?


看到众多答案中 这是我最服的答案 没有之一!


#include <iostream>#include <string.h>using namespace std;int main(){    string s;    while(cin >> s){        if(s.length() >= 1 && s.length() <= 1000){            for(int i = 0; i < s.length(); i++)                if(s[i] >= 'a' && s[i] <= 'z')                    cout << s[i];            for(int i = 0; i < s.length(); i++)                if(s[i] <= 'Z' && s[i] >= 'A')                    cout << s[i];            cout << endl;//开始没写这个老是通不过...        }    }    return 0;}



下面的也比较机智:

#include<iostream> #include<algorithm> using namespace std;    bool isLower(charc) {     returnc>='a'; }    intmain() {     string s;     while(cin >> s) {         stable_partition(s.begin(),s.end(),isLower);         cout << s << endl;     } }


0 0
原创粉丝点击