字符移位

https://www.nowcoder.com/question/next?pid=1725829&qid=44802&tid=20692948

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

冒泡思想

#include <iostream>
using namespace std;
int main(){
    string s;
    int n;
    while(cin>>s){
        n = s.length();
        for(int i=0;i<n;i++){
            for(int j=0; j< n-i-1;j++){
                if( (s[j+1]>'Z' || s[j+1]<'A') && ('A'<=s[j] && s[j]<='Z'))
                // 注意 没有 'A'<=s[j]<='Z' 这种写法!!! 被困了很久。
                  swap(s[j],s[j+1]);
            }
                
        }
        cout<<s<<endl;
    }
    
    return 0;
}

猜你喜欢

转载自blog.csdn.net/YQMind/article/details/85756906