冒泡排序 给定的字符串为:A,2.d?3!e4r87we79... 按类别排序

本文介绍了一个C++程序,该程序通过冒泡排序算法对输入字符串中的字母数字与标点符号进行解析并重新排序,确保所有字母按原顺序保留在字符串前端,而数字与标点符号则按原顺序保留在字符串后端。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include <ctype.h>
using namespace std;
/*
a.      不能改函数声明;
b.      不改变字母数字等在字符串中原有的出现顺序; 
c.      直接使用pstr所值指缓冲区,不允许另开缓冲区。 
  例如:给定的字符串为:A,2.d?3!e4r87we79...     
输出结果为:Aderwe2348779,.?!...
*/
//冒泡排序
void ParseString(char* pstr)
{
    if(pstr == NULL)
        return;
    int len = strlen(pstr);
//     for(int i=len-1; i>0; i--)
//     {
//         for(int j=0; j<i; j++)
//         {
//
//             if( ( !isalpha(pstr[j]) && isalpha(pstr[j+1]) ) ||
//                 ( ispunct(pstr[j]) && !ispunct(pstr[j+1]) )
//               )
//             {
//                 char t = pstr[j];
//                 pstr[j] = pstr[j+1];
//                 pstr[j+1] = t;
//             }
//         }
//     }
    for(int i=0; i<len-1; i++)
    {
        for(int j=len-2; j>i; j--)
        {
            if( ( !isalpha(pstr[j]) && isalpha(pstr[j+1]) ) ||
                ( ispunct(pstr[j]) && !ispunct(pstr[j+1]) )
                )
            {
                char t = pstr[j];
                pstr[j] = pstr[j+1];
                pstr[j+1] = t;
            }
        }
    } 
    cout<<pstr<<endl;
}

void main()
{
    char szText[] = "A,2.d?3!e4r87we79...";
    ParseString(szText);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值