// 1.cpp : Defines the entry point for the console application.
//华为上机测试
/*题目描述:编写一个程序,将输入字符串中的字符按如下规则排序。
规则 1 :英文字母从 A 到 Z 排列,不区分大小写。
如,输入: Type 输出: epTy
规则 2 :同一个英文字母的大小写同时存在时,按照输入顺序排列。
如,输入: BabA 输出: aABb
规则 3 :非英文字母的其它字符保持原来的位置。
如,输入: By?e 输出: Be?y
样例:
输入:
A Famous Saying: Much Ado About Nothing(2012/8).
输出:
A aaAAbc dFgghh : iimM nNn oooos Sttuuuy (2012/8).
示例1
输入
A Famous Saying: Much Ado About Nothing (2012/8).
输出
A aaAAbc dFgghh: iimM nNn oooos Sttuuuy (2012/8).
*
*作者:钱金柱
*单位:浙江大学
*日期:2019.6.3 13:54
*/
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
string instr;
vector<char> outstr;
while (getline(cin, instr))
{
int len = instr.length();
outstr.clear();//清理下数组
//第一步:将输入的字符串中的字母按照规则排列,输入数组中
for (int i = 0; i < 26; i++)
{
for (int j = 0; j < len; j++)
{
if (instr[j] - 'A' == i || instr[j] - 'a' == i)//输入字符串中的同一字母大小写位置不动(规则2)
{
outstr.push_back(instr[j]);
}
}
}
//第二步:判断输入字符串中每一个字符,若为字母输出对应的outstr;否则输出原字符
int cnt = 0;
for (int i = 0; i < len; i++)
{
if (isalpha(instr[i]))
{
cout << outstr[cnt];
cnt++;
}
else
{
cout << instr[i];
}
}
cout << endl;
}
system("pause");
return 0;
}