原题目:
给出一个字符串,该字符串是由大写的字母构成,要求你编写函数实现:把大写字母放在后面而小写字母放在前面,小写字母与大写字母各自都按照由小到大进行排列。例如:给出的原始字符串是“AaZdEBbyCzc”,则整理出来的字符串是“abcgzABCEZ”函数原型 void MoveString(char*str)。
思路:
1.先用strlen求出字符串大小
2.再用冒泡排序将字符从大到小排列好
3.用for循环和if判断语句将a到z的字符从后面换到前面
4.再用for循环将后面的A-Z字符顺序排列好
#include<iostream>
using namespace std;
int t;
void movestring(char *str)
{
int weizhi = 0;
t = strlen(str);
for (int i = 0; i < t - 1; i++)
{
for (int j = 0; j < t - i - 1; j++)
{
int h = 0;
if (str[j] > str[j + 1])
{
h = str[j];
str[j] = str[j + 1];
str[j + 1] = h;
}
}
}
for (int i = 0; i < t; i++)
{
int h = 0;
if (str[i] >= 'a'&&str[i] <= 'z')
{
h = str[weizhi];
str[weizhi] = str[i];
str[i] = h;
weizhi++;
}
}
//cout << weizhi<<endl;
for (int i = weizhi; i < t - 1; i++)
{
for (int j =weizhi; j < t - i - 1+weizhi; j++)
{
int h = 0;
//cout << j << endl;
if (str[j] > str[j + 1])
{
h = str[j];
str[j] = str[j + 1];
str[j + 1] = h;
//cout << str[j] << endl;
}
}
}
}
int main()
{
char buf[20] = "AaZdEBbyCzc";
movestring(buf);
for (int i = 0; i < t; i++)
{
cout << buf[i] << endl;
}
return 0;
}