注意:該作者博客已遷移至https://buxianshan.xyz
小Q最近遇到了一個難題:把一個字符串的大寫字母放到字符串的後面,各個字符的相對位置不變,且不能申請額外的空間。
你能幫幫小Q嗎?
輸入描述:
輸入數據有多組,每組包含一個字符串s,且保證:1<=s.length<=1000.
輸出描述:
對於每組數據,輸出移位後的字符串。
示例1
輸入:
AkleBiCeilD
輸出:
AkleBiCeilD
C++代碼:
#include<iostream>
#include<string.h>
using namespace std;
int main() {
char str[1000];
while (cin >> str) {
//若相鄰的字符左邊是大寫右邊是小寫,則交換位置。
for (int i = 0; i < strlen(str); i++) {
//迭代次數爲字符串的長度。
for (int j = 0; j < strlen(str) -1; j++) {
if (str[j] >= 'A'&&str[j]<='Z'&&str[j + 1] >= 'a'&&str[j+1]<='z') {
char t = str[j];
str[j] = str[j + 1];
str[j + 1] = t;
}
}
}
cout << str << endl;
}
return 0;
}