题目:
编写一个程序,将输入字符串中的字符按如下规则排序。
规则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).
源码:
******************************************************************************
File Name :
Version :
Author :
Created : 2014/3
Last Modified :
Description :
Function List :
History :
1.Date : 2014/3
Author :
Modification: Created file
******************************************************************************/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
/*****************************************************************************
Description : 对输入字符串进行排序
Input Param : str 待排序的字符串
Output Param : str 排序后的字符串
Return Value : 成功返回0,失败返回-1(比如,str为NULL)
*****************************************************************************/
int sort_string(char* str)
{
char SwapChar = '0';
int length = 0;
int i , j , k;
if (NULL == str)
{
return -1;
}
length = (int)strlen(str);
for(i = 0; i < length; i++)
{
for(j = 0; j < length-1-i; j++)
{
if(toupper(str[j]) < 'A' || toupper(str[j]) > 'Z')
{
continue;
}
k = j + 1;
while((toupper(str[k]) < 'A' || toupper(str[k]) > 'Z') && k < length)
{
k++;
}
if(k == length)
{
continue;
}
if(toupper(str[j]) > toupper(str[k]))
{
SwapChar = str[j];
str[j] = str[k];
str[k] = SwapChar;
}
}
}
return 0;
}