小Q最近遇到了一個難題:把一個字符串的大寫字母放到字符串的后面,各個字符的相對位置不變,且不能申請額外的空間。
你能幫幫小Q嗎?
輸入描述:
輸入數據有多組,每組包含一個字符串s,且保證:1<=s.length<=1000.
輸出描述:
對於每組數據,輸出移位后的字符串。
輸入例子:
AkleBiCeilD
輸出例子:
kleieilABCD
#include<string> #include<iostream> using namespace std; void move(string &str) { for(int i=str.size()-1;i>=0;i--) { if(str[i]>='A'&&str[i]<='Z') continue; else { int begin=i; while(str[begin]>='a'&&str[begin]<='z'&&begin>=0) { begin--; } if(begin>=0) { char ch=str[begin]; for(int j=begin+1;j<=i;j++) { str[j-1]=str[j]; } str[i]=ch; } } } } int main() { string str; while(cin>>str) { move(str); cout<<str<<endl; } }