华为OJ 字符串排序

写完之后,总觉得有点复杂。。
要求:
编写一个程序,将输入字符串中的字符按如下规则排序。
规则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).

#include <iostream>
#include <string>
using namespace std;


void strSort(char *str){
    int length = strlen(str);
    char temp;
    for (int i = 0; i < length; i++)
    {
        for (int j = 0; j < length - i - 1; j++){
            if (!(str[j] - str[j + 1] == 32 || str[j + 1] - str[j] == 32))
            {
                if (str[j] >= 'a'&&str[j] <= 'z'&&str[j + 1] >= 'a'&&str[j + 1] <= 'z')
                {
                    if (str[j] - 32>str[j + 1] - 32)
                    {
                        temp = str[j];
                        str[j] = str[j + 1];
                        str[j + 1] = temp;
                    }
                }
                else if (str[j] >= 'a'&&str[j] <= 'z')
                {
                    if (str[j] - 32 > str[j + 1])
                    {
                        temp = str[j];
                        str[j] = str[j + 1];
                        str[j + 1] = temp;
                    }
                }
                else if (str[j + 1] >= 'a'&&str[j + 1] <= 'z')
                {
                    if (str[j] > str[j + 1] - 32)
                    {
                        temp = str[j];
                        str[j] = str[j + 1];
                        str[j + 1] = temp;
                    }
                }
                else{
                    if (str[j] > str[j + 1] - 32)
                    {
                        temp = str[j];
                        str[j] = str[j + 1];
                        str[j + 1] = temp;
                    }
                }

            }
        }
    }
}

int main()
{
    //字符串排序
    string str;
    getline(cin, str);
    char *outPutStr = new char[str.size() + 1];
    char *temp = new char[str.size() + 1];
    for (int i = 0; i < str.size(); i++)
    {
        outPutStr[i] = 'a';
    }
    outPutStr[str.size()] = '\0';
    int index = 0;
    //先把除字母意外的字符放在最终的位置上
    for (int i = 0; i < str.size(); i++)
    {
        if (str[i]<'A' || (str[i]>'Z'&&str[i]<'a') || str[i]>'z')
        {
            outPutStr[i] = str[i];
        }
        else{
            temp[index] = str[i];
            index++;
        }
    }
    temp[index] = '\0';

    //对temp排序
    strSort(temp);
    index = 0;
    for (int i = 0; i < str.size(); i++)
    {
        if (outPutStr[i] == 'a')
        {
            outPutStr[i] = temp[index++];
        }
    }
    cout << outPutStr << endl;
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值