有一个由大小写组成的字符串,现在需要对它进行修改,将其中的所有小写字母排在大写字母的前面(大写或小写字母之间不要求保持原来次序)。 #include <cstdlib> #include <iostream> #include <algorithm> using namespace std; const int MAX = 100; void handleStr(char *str) { int size = strlen(str); int head = 0; int tail = size - 1; while(head < tail) { while(*(str + head) < 'A' || *(str + head) > 'Z') head++; while(*(str + tail) < 'a' || *(str + tail) > 'z') tail--; swap(*(str + head), *(str + tail)); } } int main(int argc, char *argv[]) { char str[MAX] = {'/0'}; cin.getline(str, 100); handleStr(str); cout << "swap result is: " << endl; cout << str << endl; system("PAUSE"); return EXIT_SUCCESS; } 还有点问题 。。。 #include <cstdlib> #include <iostream> #include <algorithm> using namespace std; bool isUpperCase(char c) { if(c >= 'A' && c <= 'Z') return true; else return false; } void hanStr(char *str) { int head = 0, end = strlen(str) - 1; while(head < end) { if(isUpperCase(str[head])) { while(isUpperCase(str[end])) end--; swap(str[head++], str[end--]); } else head++; } } int main(int argc, char *argv[]) { char a[] = {"abcdefgHIGKLMNopqistUV"}; hanStr(a); cout << a << endl; system("PAUSE"); return EXIT_SUCCESS; }