描述
编写一个程序,将输入字符串中的字符按如下规则排序。
**规则 1 :**英文字母从 A 到 Z 排列,不区分大小写。
如,输入: Type 输出: epTy
**规则 2 :**同一个英文字母的大小写同时存在时,按照输入顺序排列。
如,输入: BabA 输出: aABb
**规则 3 :**非英文字母的其它字符保持原来的位置。
如,输入: By?e 输出: Be?y
**数据范围:**输入的字符串长度满足 1≤n≤1000
输入描述:
输入字符串
输出描述:
输出字符串
示例1
输入: A Famous Saying: Much Ado About Nothing (2012/8).
输出: A aaAAbcdFgghh: iimM nNn oooos Sttuuuy (2012/8).
实现
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
bool isNeedSort(char c)
{
if ((c >= 'a') && (c <= 'z') ||
(c >= 'A') && (c <= 'Z')) {
return true;
} else {
return false;
}
}
void swap(char *m, char *n)
{
char tmp = *m;
*m = *n;
*n = tmp;
}
void extractStr(char *inBuf, char *outBuf)
{
int index = 0;
int len = strlen(inBuf);
for (int i = 0; i < len; i++) {
if (isNeedSort(inBuf[i])) {
outBuf[index++] = inBuf[i];
}
}
}
void BubbleSort(char *pBuf, int n)
{
int flag;
int i = 0, j = 0;
for (i = 0; i < n - 1; i++) //共进行n-1次
{
flag = 1;
for (j = 0; j < n - 1 - i; j++) { // 每一次找出最大的放最后面
if (tolower(pBuf[j]) > tolower(pBuf[j+1])) {
swap(&pBuf[j], &pBuf[j+1]);
flag = 0;
}
}
// printf("after sort i: %d. %s\n", i, pBuf);
if (flag == 1) {
// printf("flag: %d, do not need sort\n", flag);
break;
}
}
}
void recoverStr(char *inBuf, char *outBuf) // 非英文字母不覆盖
{
int index = 0;
int len = strlen(outBuf);
for (int i = 0; i < len; i++) {
if (isNeedSort(outBuf[i])) {
outBuf[i] = inBuf[index++];
}
}
}
/*
* 先提取不需要排序的非英文字母 - extractStr,再对英文字母进行排序 - BubbleSort;
* 排好序后,再回填原来的字符串 - recoverStr
*/
int main()
{
char strBuf[1001] = {0};
char tmpBuf[1001] = {0};
gets(strBuf);
// printf("strBuf: %s\n", strBuf);
extractStr(strBuf, tmpBuf); // 去掉非英文字母
// printf("tmpBuf: %s\n", tmpBuf);
BubbleSort(tmpBuf, strlen(tmpBuf)); // 对英文字母进行排序
recoverStr(tmpBuf, strBuf);
printf("%s\n", strBuf);
return 0;
}
调试
strBuf: A Famous Saying: Much Ado About Nothing (2012/8).
tmpBuf: AFamousSayingMuchAdoAboutNothing
after sort i: 0. AaFmosSauingMuchAdoAboutNothingy
after sort i: 1. AaFmosaSingMuchAdoAboutNothinguy
after sort i: 2. AaFmoasingMSchAdoAboutNothinguuy
after sort i: 3. AaFmaoingMschAdoAboStNothinguuuy
after sort i: 4. AaFamingMochAdoAbosSNothingtuuuy
after sort i: 5. AaaFimgMnchAdoAboosNoShingttuuuy
after sort i: 6. AaaFigmMchAdnAboooNoshingSttuuuy
after sort i: 7. AaaFgimchAdMAbnooNoohingsSttuuuy
after sort i: 8. AaaFgichAdmAbMnoNoohingosSttuuuy
after sort i: 9. AaaFgchAdiAbmMnNoohingoosSttuuuy
after sort i: 10. AaaFcgAdhAbimMnNohingooosSttuuuy
after sort i: 11. AaacFAdgAbhimMnNhingoooosSttuuuy
after sort i: 12. AaacAdFAbghimMnhiNgnoooosSttuuuy
after sort i: 13. AaaAcdAbFghimMhingNnoooosSttuuuy
after sort i: 14. AaaAcAbdFghimhiMgnNnoooosSttuuuy
after sort i: 15. AaaAAbcdFghihimgMnNnoooosSttuuuy
after sort i: 16. AaaAAbcdFghhiigmMnNnoooosSttuuuy
after sort i: 17. AaaAAbcdFghhigimMnNnoooosSttuuuy
after sort i: 18. AaaAAbcdFghhgiimMnNnoooosSttuuuy
after sort i: 19. AaaAAbcdFghghiimMnNnoooosSttuuuy
after sort i: 20. AaaAAbcdFgghhiimMnNnoooosSttuuuy
after sort i: 21. AaaAAbcdFgghhiimMnNnoooosSttuuuy
flag: 1, do not need sort
A aaAAbc dFgghh: iimM nNn oooos Sttuuuy (2012/8).