HJ26 字符串排序
描述:
编写一个程序,将输入字符串中的字符按如下规则排序。
规则 1 :英文字母从 A 到 Z 排列,不区分大小写。 如,输入: Type 输出: epTy
规则 2 :同一个英文字母的大小写同时存在时,按照输入顺序排列。如,输入: BabA 输出: aABb
规则 3 :非英文字母的其它字符保持原来的位置。 如,输入: By?e 输出: Be?y
数据范围:输入的字符串长度满足 1≤n≤1000
输入描述:输入字符串 输出描述:输出字符串
> 输入:A Famous Saying: Much Ado About Nothing (2012/8).
> 输出:A aaAAbc dFgghh: iimM nNn oooos Sttuuuy (2012/8).
public class Program
{
public static void Main()
{
string str;
str = System.Console.ReadLine();
string s = str;
for (int i = 0; i < str.Length; i++)
{
if (str[i] < 'A' || (str[i] > 'Z' && str[i] < 'a') || str[i] > 'z')
{
str=str.Remove(i, 1);
i--;
}
}
for (int i = 0; i < s.Length; i++)
{
if (s[i] < 'A' || (s[i] > 'Z' && s[i] < 'a') || s[i] > 'z')
{
continue;
}
int k = 0;
char q = str[0];
int c = str.Length;
for (int j = 1; j < c; j++)
{
if (char.ToUpper(str[j]) < char.ToUpper(q))
{
q = str[j];
k = j;
}
}
s=s.Remove(i,1).Insert(i, q.ToString());
str=str.Remove(k, 1);
}
System.Console.WriteLine(s);
}
}